class inheritance liang, chpt 8. x is a y: the is-a hierarchy life-form animal person clown...

23
Class Inheritance Liang, Chpt 8

Upload: paola-stallsworth

Post on 15-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

Class Inheritance

Liang, Chpt 8

Page 2: 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

X is a Y: the is-a hierarchy

life-form

animal

person

clown

geometric object

circle

cylinder

instrument

trumpet

piano

Page 3: 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)

Page 4: 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

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

Page 5: 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

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()......

}

Page 6: 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

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

Page 7: 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

Reminder: constructors for Circle// default constructor

public Circle()

{

radius = 1.0;

}

// Construct Circle with specific radius

public Circle(double r)

{

radius = r;

}

Page 8: 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

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;

}

Page 9: 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

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

Page 10: 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

UML diagram

Circle

-radius

+getRadius

+setRadius

+findArea

Cylinder

-length

+getLength

+setLength

+findVolume

Page 11: 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

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"

Page 12: 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

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

Page 13: 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

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.

Page 14: 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

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;

}

Page 15: 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

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

Page 16: 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

Example of overriding

// in class Cylinder

public double findArea()

{

return 2 * super.findArea() +

(2*getRadius()*Math.PI) * length;

}

Page 17: 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

Another toy example

Instrument: "toot toot"

Saxophone extends Instrument: "honk honk" (overrides play)

Trumpet extends Instrument: calls super.play() and adds to it

Page 18: 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

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)

Page 19: 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

Hierarchy again

Object

Instrument

Saxophone Trombone

Circle

Cylinder

Page 20: 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

Methods inherited from Object

• public boolean equals(Object O)• public String toString()• and others (clone, etc)

Page 21: 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

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

Page 22: 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

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

Page 23: 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

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);