The
old way of
re-using
existing programs
- Suppose
we
need to
write a Rectangle class
and we found
the
Circle class
that looks
similar:
public class Circle
{
private String color;
private double radius;
Circle(String col, double r)
{
color = col; radius = r;
}
public String getColor() { return color; }
public void setColor(String c) { color = c; }
public double getRadius() { return radius;}
public void setRadius(double r) { radius = r; }
public double getArea()
{
return 3.14159*radius*radius;
}
}
|
public class Rectangle
{
We need to write this class
}
|
|
Comment:
I have
simplified the
example in the
text book
(uses fewer
variables and
methods)
The
old way of
re-using
existing programs
- The old way:
(1)
copy the
variables and
methods in the
Circle class:
public class Circle
{
private String color;
private double radius;
Circle(String col, double r)
{
color = col; radius = r;
}
public String getColor() { return color; }
public void setColor(String c) { color = c; }
public double getRadius() { return radius;}
public void setRadius(double r) { radius = r; }
public double getArea()
{
return 3.14159*radius*radius;
}
}
|
public class Rectangle
{
private String color;
private double radius;
// Do not copy constructors
// Make new one(s) !!
public String getColor() { return color; }
public void setColor(String c) { color = c; }
public double getRadius() { return radius;}
public void setRadius(double r) { radius = r; }
public double getArea()
{
return 3.14159*radius*radius;
}
}
|
|
Comment:
I have
simplified the
example in the
text book
(uses fewer
variables and
methods)
The
old way of
re-using
existing programs
- The old way:
(2)
make
changes to
the variables and
methods:
public class Circle
{
private String color;
private double radius;
Circle(String col, double r)
{
color = col; radius = r;
}
public String getColor() { return color; }
public void setColor(String c) { color = c; }
public double getRadius() { return radius;}
public void setRadius(double r) { radius = r; }
public double getArea()
{
return 3.14159*radius*radius;
}
}
|
public class Rectangle
{
private String color;
private double width;
private double height;
Rectangle(String col, double w, double h)
{
color = col; width = w; height = h;
}
public String getColor() { return color; }
public void setColor(String c) { color = c; }
public double getWidth() { return width;}
public void setWidth(double r) { width = r; }
public double getHeight() { return height;}
public void setHeight(double r) { height = r; }
public double getArea()
{
return width*height;
}
}
|
|
Comment:
I have
simplified the
example in the
text book
(uses fewer
variables and
methods)
The
old way of
re-using existing programs
-
Notice:
there are
duplication of
variable(s) and/or
methods in the
programs:
public class Circle
{
private String color;
private double radius;
Circle(String col, double r)
{
color = col; radius = r;
}
public String getColor() { return color; }
public void setColor(String c) { color = c; }
public double getRadius() { return radius;}
public void setRadius(double r) { radius = r; }
public double getArea()
{
return 3.14159*radius*radius;
}
}
|
public class Rectangle
{
private String color;
private double width;
private double height;
Rectangle(String col, double w, double h)
{
color = col; width = w; height = h;
}
public String getColor() { return color; }
public void setColor(String c) { color = c; }
public double getWidth() { return width;}
public void setWidth(double r) { width = r; }
public double getHeight() { return height;}
public void setHeight(double r) { height = r; }
public double getArea()
{
return width*height;
}
}
|
|
Duplication
makes program
maintainance
difficult
(e.g.,
changing
setColor( )
⇒
make changes in
2 places)
Test program on
Circle
and
Rectangle
objects
public class myProg
{
public static void main(String[] args)
{
Circle c1 = new Circle("red", 2);
Circle c2 = new Circle("blue", 4);
Rectangle r1 = new Rectangle("green", 2, 3);
Rectangle r2 = new Rectangle("red", 1, 4);
System.out.println( c1.getColor() + " " + c1.getArea() );
System.out.println( c2.getColor() + " " + c2.getArea() );
System.out.println( r1.getColor() + " " + r1.getArea() );
System.out.println( r2.getColor() + " " + r2.getArea() );
}
}
|
DEMO:
demo/13-inheritance/04-copy+paste
Let's see
how we can
define a
suitable
superclass for
Circle
and
Rectangle
that allow us to
remove
duplication !
Applying
OOP:
an
object oriented design
example
-
Find the
superclass:
we find the
common (= shared)
properties(= variables) and
actions (= methods)
in the classes:
public class Circle
{
private String color;
private double radius;
Circle(String col, double r)
{
color = col; radius = r;
}
public String getColor() { return color; }
public void setColor(String c) { color = c; }
public double getRadius() { return radius;}
public void setRadius(double r) { radius = r; }
public double getArea()
{
return 3.14159*radius*radius;
}
}
|
public class Rectangle
{
private String color;
private double width;
private double height;
Rectangle(String col, double w, double h)
{
color = col; width = w; height = h;
}
public String getColor() { return color; }
public void setColor(String c) { color = c; }
public double getWidth() { return width;}
public void setWidth(double r) { width = r; }
public double getHeight() { return height;}
public void setHeight(double r) { height = r; }
public double getArea()
{
return width*height;
}
}
|
|
•
We define a
super class
containing
all these
common members
Applying
OOP:
an
object oriented design
example
- The superclass that
contains
the
common (shared)
properties
and
actions
in the
classes
Circle and
Rectangle are:
|
Comment:
some
methods
(e.g.:
getArea( )) in the
superclass
may not have
a useful
method body ---
it's OK.
We will learn (later) that this
it is very useful for the
polymorphism mechanism !!
Applying
OOP:
an
object oriented design
example
- We change the
Circle
and Rectangle
classes and
make them
extend the
superclass
GeometricObject:
public class Circle extends GeometricObject
{
private String color;
private double radius;
Circle(String col, double r)
{
color = col; radius = r;
}
public String getColor() { return color; }
public void setColor(String c) { color = c; }
public double getRadius() { return radius;}
public void setRadius(double r) { radius = r; }
public double getArea()
{
return 3.14159*radius*radius;
}
}
|
public class Rectangle extends GeometricObject
{
private String color;
private double width;
private double height;
Rectangle(String col, double w, double h)
{
color = col; width = w; height = h;
}
public String getColor() { return color; }
public void setColor(String c) { color = c; }
public double getWidth() { return width;}
public void setWidth(double r) { width = r; }
public double getHeight() { return height;}
public void setHeight(double r) { height = r; }
public double getArea()
{
return width*height;
}
}
|
|
•
The classes will
inherit:
(1) color,
(2) getColor(),
(3) setColor() and
(4) getArea()
Applying
OOP:
an
object oriented design
example
- The
String color
variable
inherited from
the superclass (GeometricObject)
is the
same
and we can
remove its
definition:
public class Circle extends GeometricObject
{
private double radius;
Circle(String col, double r)
{
color = col; radius = r;
}
public String getColor() { return color; }
public void setColor(String c) { color = c; }
public double getRadius() { return radius;}
public void setRadius(double r) { radius = r; }
public double getArea()
{
return 3.14159*radius*radius;
}
}
|
public class Rectangle extends GeometricObject
{
private double width;
private double height;
Rectangle(String col, double w, double h)
{
color = col; width = w; height = h;
}
public String getColor() { return color; }
public void setColor(String c) { color = c; }
public double getWidth() { return width;}
public void setWidth(double r) { width = r; }
public double getHeight() { return height;}
public void setHeight(double r) { height = r; }
public double getArea()
{
return width*height;
}
}
|
|
•
The classes will
inherit:
(1) color,
(2) getColor(),
(3) setColor() and
(4) getArea()
Applying
OOP:
an
object oriented design
example
- The
getColor()
and
setColor()
methods
inherited
are also
the
same
and we can
remove their
definitions:
public class Circle extends GeometricObject
{
private double radius;
Circle(String col, double r)
{
color = col; radius = r;
}
public double getRadius() { return radius;}
public void setRadius(double r) { radius = r; }
public double getArea()
{
return 3.14159*radius*radius;
}
}
|
public class Rectangle extends GeometricObject
{
private double width;
private double height;
Rectangle(String col, double w, double h)
{
color = col; width = w; height = h;
}
public double getWidth() { return width;}
public void setWidth(double r) { width = r; }
public double getHeight() { return height;}
public void setHeight(double r) { height = r; }
public double getArea()
{
return width*height;
}
}
|
|
•
The classes will
inherit:
(1) color,
(2) getColor(),
(3) setColor() and
(4) getArea()
Applying
OOP:
an
object oriented design
example
- The
getArea()
method is
different from
the GeometricObject,
so
we
override
it with a
new
getArea() method in
the subclass:
public class Circle extends GeometricObject
{
private double radius;
Circle(String col, double r)
{
color = col; radius = r;
}
public double getRadius() { return radius;}
public void setRadius(double r) { radius = r; }
public double getArea() // Overrides
{
return 3.14159*radius*radius;
}
}
|
public class Rectangle extends GeometricObject
{
private double width;
private double height;
Rectangle(String col, double w, double h)
{
color = col; width = w; height = h;
}
public double getWidth() { return width;}
public void setWidth(double r) { width = r; }
public double getHeight() { return height;}
public void setHeight(double r) { height = r; }
public double getArea() // Overrides
{
return width*height;
}
}
|
|
•
The classes will
inherit:
(1) color,
(2) getColor(),
(3) setColor() and
(4) getArea()
Applying
OOP:
an
object oriented design
example
- There is one problem:
(can you spot it ?)
public class Circle extends GeometricObject
{
private double radius;
Circle(String col, double r)
{
color = col; radius = r;
}
public double getRadius() { return radius;}
public void setRadius(double r) { radius = r; }
public double getArea() // Overrides
{
return 3.14159*radius*radius;
}
}
|
public class Rectangle extends GeometricObject
{
private double width;
private double height;
Rectangle(String col, double w, double h)
{
color = col; width = w; height = h;
}
public double getWidth() { return width;}
public void setWidth(double r) { width = r; }
public double getHeight() { return height;}
public void setHeight(double r) { height = r; }
public double getArea() // Overrides
{
return width*height;
}
}
|
|
•
Problem:
color
is
private
variable in
GeometricObject and
cannot be
accessed
directly
Applying
OOP:
an
object oriented design
example
- Replace all
direct access to
private
members
in superclass with
calls to
accessor/mutator methods:
public class Circle extends GeometricObject
{
private double radius;
Circle(String col, double r)
{
setColor(col); radius = r;
}
public double getRadius() { return radius;}
public void setRadius(double r) { radius = r; }
public double getArea() // Overrides
{
return 3.14159*radius*radius;
}
}
|
public class Rectangle extends GeometricObject
{
private double width;
private double height;
Rectangle(String col, double w, double h)
{
setColor(col); width = w; height = h;
}
public double getWidth() { return width;}
public void setWidth(double r) { width = r; }
public double getHeight() { return height;}
public void setHeight(double r) { height = r; }
public double getArea() // Overrides
{
return width*height;
}
}
|
|
•
Notice:
now there is
no duplication of
variable defintions or
methods !
Test program on
Circle
and
Rectangle
objects
public class myProg
{
public static void main(String[] args)
{
Circle c1 = new Circle("red", 2);
Circle c2 = new Circle("blue", 4);
Rectangle r1 = new Rectangle("green", 2, 3);
Rectangle r2 = new Rectangle("red", 1, 4);
System.out.println( c1.getColor() + " " + c1.getArea() );
System.out.println( c2.getColor() + " " + c2.getArea() );
System.out.println( r1.getColor() + " " + r1.getArea() );
System.out.println( r2.getColor() + " " + r2.getArea() );
}
}
|
DEMO:
demo/13-inheritance/05-OOD
This test program
works like before !!!
❮
❯