Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Class Inheritance Liang, Chpt 8

2 X is a Y: the is-a hierarchy life-form animal person clown geometric object circle cylinder instrument trumpet piano

3 Superclass/Subclass piece of furniture sofa superclass subclass (base class, parent class) (child class, derived class)

4 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

5 Reminder: the Circle class public class Circle { private double radius; public Circle()..... public Circle(double r).... public double getRadius().... public void setRadius(double newRadius).... public double findArea()...... }

6 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

7 Reminder: constructors for Circle // default constructor public Circle() { radius = 1.0; } // Construct Circle with specific radius public Circle(double r) { radius = r; }

8 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; }

9 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

10 UML diagram Circle -radius +getRadius +setRadius +findArea Cylinder -length +getLength +setLength +findVolume

11 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 "

12 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

13 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.

14 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; }

15 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

16 Example of overriding // in class Cylinder public double findArea() { return 2 * super.findArea() + (2*getRadius()*Math.PI) * length; }

17 Another toy example Instrument: "toot toot" Saxophone extends Instrument: "honk honk" (overrides play) Trumpet extends Instrument: calls super.play() and adds to it

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

19 Hierarchy again Object Instrument Saxophone Trombone Circle Cylinder

20 Methods inherited from Object public boolean equals(Object O) public String toString() and others (clone, etc)

21 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

22 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

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


Download ppt "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."

Similar presentations


Ads by Google