class inheritance liang, chpt 8. x is a y: the is-a hierarchy life-form animal person clown...
TRANSCRIPT
Class Inheritance
Liang, Chpt 8
X is a Y: the is-a hierarchy
life-form
animal
person
clown
geometric object
circle
cylinder
instrument
trumpet
piano
Superclass/Subclass
piece of furniture
sofa
superclass
subclass
(base class,
parent class)
(child class,
derived class)
Inheritance
• An instance of a subclass is an instance of its superclass
• A subclass inherits methods and variables from its superclass
• A subclass adds to the superclass: it is an extension of the superclass
Reminder: the Circle classpublic class Circle
{
private double radius;
public Circle().....
public Circle(double r)....
public double getRadius()....
public void setRadius(double newRadius)....
public double findArea()......
}
Cylinder as a special kind of Circle
public class Cylinder extends Circle
{
private double length;
public double getLength()....
public void setLength(double newLength)...
public double findVolume()...
}+ constructors
Reminder: constructors for Circle// default constructor
public Circle()
{
radius = 1.0;
}
// Construct Circle with specific radius
public Circle(double r)
{
radius = r;
}
Constructors for Cylinder// default constructor
public Cylinder()
{
super();
length = 1.0;
}
// Constructor with expicit length and radius
public Cylinder(double r, double l)
{
super(r);
length = l;
}
Using a Cylinder
Cylinder myCyl = new Cylinder(5.0, 2.0);
System.out.println("length: " + myCyl.getLength());
System.out.println("radius: " + myCyl.getRadius());
System.out.println("volume: " + myCyl.findVolume());
System.out.println("area: " + myCyl.findArea());
Program TestCylinder, Example 6.1
UML diagram
Circle
-radius
+getRadius
+setRadius
+findArea
Cylinder
-length
+getLength
+setLength
+findVolume
Reminder: using this in methods
• this refers to the present object
class Foo
{
int i = 5;
void setI(int i)
{
this.i = i;
}
}
"Assign argument i to the object's instance variable i"
Reminder: using this(..) in constructors
• this(...) in a constructor calls another constructor for this class
public Circle(double radius) {
this.radius = radius;
}
public Circle() {
this(1.0);
}
Note: a call to this(...) must be the first statement in the constructor
Using super• Like this, super can be used to call a method or to call a constructor
• If used like super.findArea(), it calls the findArea method of the superclass
• If used like super(1.0), it calls the corresponding constructor of the superclass
• If used as super(1.0) in a constructor, it must be the first statement of the constructor.
Example: using super in a constructor
// default constructor
public Cylinder() {
super(); // call default const. of superclass
this.length = 1.0;
}
// another constructor
public Cylinder(double r, length l) {
super(r);
this.length = l;
}
Overriding methods
• Sometimes a subclass may choose to re-define a method which it inherits
• If it does so, the new method (e.g. in Cylinder) overrides the corresponding method of the superclass (Circle)
• E.g. findArea() in class Cylinder should really return the surface area of the cylinder
Example of overriding
// in class Cylinder
public double findArea()
{
return 2 * super.findArea() +
(2*getRadius()*Math.PI) * length;
}
Another toy example
Instrument: "toot toot"
Saxophone extends Instrument: "honk honk" (overrides play)
Trumpet extends Instrument: calls super.play() and adds to it
Object class
• Every class has a superclass
• If a class does not explicitly extend another class, its superclass is Object
• Class Object defines a few, generic methods
• These methods should usually be overridden by subclasses (by all classes)
Hierarchy again
Object
Instrument
Saxophone Trombone
Circle
Cylinder
Methods inherited from Object
• public boolean equals(Object O)• public String toString()• and others (clone, etc)
The equals method
•Purpose: to test whether two objects of the same type are "equal" in some meaningful sense
•Default behaviour: test for equality of references
•Example of overridden method: String.equals
•Exercise: add an equals method to Circle
•Habit for life: always define an equals method
The toString method
• Purpose: return a String representation of an object, e.g. for printing to the console
• Default behaviour: print classname and address of the object reference
• Better behaviour: print something sensible and informative
• Example: Circle[radius=1.0]• Exercise: examine default behaviour and modify it• Most common use: implicit call in String
concatenation
Implicit calling of toString()
The following two groups of statements are equivalent:
Circle c = new Circle();
String s = c.toString();
System.out.println("Here: " + s);
Circle c = new Circle();
System.out.println("Here: " + c);