Chapter 9 ObjectOriented Programming Inheritance Outline 9 1
Chapter 9 - Object-Oriented Programming: Inheritance Outline 9. 1 9. 2 9. 3 9. 4 9. 5 9. 6 9. 7 Introduction Superclasses and Subclasses protected Members Relationship between Superclasses and Subclasses Case Study: Three-Level Inheritance Hierarchy Constructors and Finalizers in Subclasses Software Engineering with Inheritance 2003 Prentice Hall, Inc. All rights reserved. 1
2 9. 1 Introduction • Inheritance – Software reusability – Create new class from existing class • Absorb existing class’s data and behaviors • Enhance with new capabilities – Subclass extends superclass • Subclass – More specialized group of objects – Behaviors inherited from superclass • Can customize – Additional behaviors 2003 Prentice Hall, Inc. All rights reserved.
3 9. 1 Introduction • Class hierarchy – Direct superclass • Inherited explicitly (one level up hierarchy) – Indirect superclass • Inherited two or more levels up hierarchy – Single inheritance • Inherits from one superclass – Multiple inheritance • Inherits from multiple superclasses – Java does not support multiple inheritance 2003 Prentice Hall, Inc. All rights reserved.
4 9. 1 Introduction • Abstraction – Focus on commonalities among objects in system • “is-a” vs. “has-a” – “is-a” • Inheritance • subclass object treated as superclass object • Example: Car is a vehicle – Vehicle properties/behaviors also car properties/behaviors – “has-a” • Composition • Object contains one or more objects of other classes as members • Example: Car has a steering wheel 2003 Prentice Hall, Inc. All rights reserved.
5 9. 2 Superclasses and Subclasses • Superclasses and subclasses – Object of one class “is an” object of another class • Example: Rectangle is quadrilateral. – Class Rectangle inherits from class Quadrilateral – Quadrilateral: superclass – Rectangle: subclass – Superclass typically represents larger set of objects than subclasses • Example: – superclass: Vehicle • Cars, trucks, boats, bicycles, … – subclass: Car • Smaller, more-specific subset of vehicles 2003 Prentice Hall, Inc. All rights reserved.
6 9. 2 Superclasses and Subclasses (Cont. ) • Inheritance examples 2003 Prentice Hall, Inc. All rights reserved.
7 9. 2 Superclasses and Subclasses (Cont. ) • Inheritance hierarchy – Inheritance relationships: tree-like hierarchy structure – Each class becomes • superclass – Supply data/behaviors to other classes OR • subclass – Inherit data/behaviors from other classes 2003 Prentice Hall, Inc. All rights reserved.
8 Community. Member Employee Faculty Administrator Fig. 9. 2 Student Alumnus Staff Teacher Inheritance hierarchy for university Community. Members. 2003 Prentice Hall, Inc. All rights reserved.
9 Shape Two. Dimensional. Shape Circle Square Triangle Fig. 9. 3 Three. Dimensional. Shape Sphere Inheritance hierarchy for Shapes. 2003 Prentice Hall, Inc. All rights reserved. Cube Tetrahedron
10 9. 3 protected Members • protected access – Intermediate level of protection between public and private – protected members accessible to • superclass members • subclass members • Class members in the same package – Subclass access superclass member • Keyword super and a dot (. ) 2003 Prentice Hall, Inc. All rights reserved.
9. 4 Relationship between Superclasses and Subclasses • Superclass and subclass relationship – Example: Point/circle inheritance hierarchy • Point – x-y coordinate pair • Circle – x-y coordinate pair – Radius 2003 Prentice Hall, Inc. All rights reserved. 11
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 // Fig. 9. 4: Point. java // Point class declaration represents an x-yx-coordinate pair. Maintain and y-coordinates Outline 12 as private instance public class Point { variables. private int x; // x part of coordinate pair private int y; // y part of coordinate pair Implicit call to // no-argument constructor Object constructor public Point() { // implicit call to Object constructor occurs here } // constructor public Point( int x. Value, int y. Value ) { // implicit call to Object constructor occurs here x = x. Value; // no need for validation y = y. Value; // no need for validation } // set x in coordinate pair public void set. X( int x. Value ) { x = x. Value; // no need for validation } Point. java Lines 5 -6 Maintain x- and ycoordinates as private instance variables. Line 11 Implicit call to Object constructor 2003 Prentice Hall, Inc. All rights reserved.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 // return x from coordinate pair public int get. X() { return x; } Outline // set y in coordinate pair public void set. Y( int y. Value ) { y = y. Value; // no need for validation } // return y from coordinate pair public int get. Y() { return y; } Point. java Override method to. String of class Object // return String representation of Point object public String to. String() { return "[" + x + ", " + y + "]"; } Lines 47 -50 Override method to. String of class Object. } // end class Point 2003 Prentice Hall, Inc. All rights reserved. 13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 // Fig. 9. 5: Point. Test. java // Testing class Point. import javax. swing. JOption. Pane; Outline 14 public class Point. Test { Instantiate Point object public static void main( String[] args ) { Point point = new Point( 72, 115 ); // create Point object // get point coordinates String output = "X coordinate is " + point. get. X() + Change the value "n. Y coordinate is " + point. get. Y(); of point’s xand y- coordinates point. set. X( 10 ); point. set. Y( 20 ); // set x-coordinate // set y-coordinate // get String representation of new point value output += "nn. The new location of point is " + point; JOption. Pane. show. Message. Dialog( null, output ); // display output System. exit( 0 ); Point. Test. java Line 9 Instantiate Point object Implicitly call point’s Lines 15 -16 method to. String Change the value of point’s x- and ycoordinates Line 19 Implicitly call point’s to. String method } // end main } // end class Point. Test 2003 Prentice Hall, Inc. All rights reserved.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 // Fig. 9. 6: Circle. java // Circle class contains x-y coordinate pair and radius. public class Circle { private int x; private int y; private double radius; Outline 15 Maintain x-y coordinates and radiusofas Circle's privatecenter x-coordinate y-coordinate Circle's center instance of variables. // // // Circle's radius // no-argument constructor public Circle() { // implicit call to Object constructor occurs here } Circle. java // constructor public Circle( int x. Value, int y. Value, double radius. Value ) { // implicit call to Object constructor occurs here x = x. Value; // no need for validation y = y. Value; // no need for validation set. Radius( radius. Value ); } // set x in coordinate pair public void set. X( int x. Value ) { x = x. Value; // no need for validation } Lines 5 -7 Maintain x- and ycoordinates and radius as private instance variables. Lines 25 -28 Note code similar to Point code. 2003 Prentice Hall, Inc. All rights reserved.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 // return x from coordinate pair public int get. X() { return x; } // set y in coordinate pair public void set. Y( int y. Value ) { y = y. Value; // no need for validation } Outline Note code similar to Point code. // return y from coordinate pair public int get. Y() { return y; } Circle. java // set radius public void set. Radius( double radius. Value ) { radius = ( radius. Value < 0. 0 ? 0. 0 : radius. Value ); } // return radius public double get. Radius() { return radius; } Lines 31 -47 Note code similar to Point code. Line 51 Ensure non-negative value for radius. 2003 Prentice Hall, Inc. All rights reserved. 16
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 // calculate and return diameter public double get. Diameter() { return 2 * radius; } Outline // calculate and return circumference public double get. Circumference() { return Math. PI * get. Diameter(); } // calculate and return area public double get. Area() { return Math. PI * radius; } Circle. java // return String representation of Circle object public String to. String() { return "Center = [" + x + ", " + y + "]; Radius = " + radius; } } // end class Circle 2003 Prentice Hall, Inc. All rights reserved. 17
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 Outline // Fig. 9. 7: Circle. Test. java // Testing class Circle. import java. text. Decimal. Format; import javax. swing. JOption. Pane; Create Circle object. public class Circle. Test { public static void main( String[] args ) { Circle circle = new Circle( 37, 43, 2. 5 ); // create Circle object // get Circle's initial x-y coordinates and radius String output = "X coordinate is " + circle. get. X() + "n. Y coordinate is " + circle. get. Y() + "n. Radius is " + circle. get. Radius(); circle. set. X( 35 ); circle. set. Y( 20 ); circle. set. Radius( 4. 25 ); 18 // set new x-coordinate // set new y-coordinate // set new radius // get String representation of output += "nn. The new location circle. to. String(); Explicitly call circle’s new to. String circle value Use setmethods to modify and radius of circleinstance aren" + private variable. Circle. Test. java Line 10 Create Circle object Lines 17 -19 Use set methods to modify private instance variable Line 23 Explicitly call circle’s to. String method // format floating-point values with 2 digits of precision Decimal. Format two. Digits = new Decimal. Format( "0. 00" ); 2003 Prentice Hall, Inc. All rights reserved.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 Outline // get Circle's diameter output += "n. Diameter is " + two. Digits. format( circle. get. Diameter() ); // get Circle's circumference output += "n. Circumference is " + two. Digits. format( circle. get. Circumference() ); 19 Use get methods to obtain circle’s diameter, circumference and area. // get Circle's area output += "n. Area is " + two. Digits. format( circle. get. Area() ); JOption. Pane. show. Message. Dialog( null, output ); // display output System. exit( 0 ); } // end main } // end class Circle. Test. java Lines 29 -37 Use get methods to obtain circle’s diameter, circumference and area. 2003 Prentice Hall, Inc. All rights reserved.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 // Fig. 9. 8: Circle 2. java // Circle 2 class inherits from Point. Class Circle 2 extends class Point. Outline 20 public class Circle 2 extends Point { private double radius; // Circle 2's radius Maintain private instance // no-argument constructor variable radius. public Circle 2() { // implicit call to Point constructor occurs here } // constructor Attempting to access superclass public Circle 2( int x. Value, int y. Value, double radius. Value ) Point’s private instance { variables and y results in syntax // implicit call to Point constructor occursxhere x = x. Value; // not allowed: x private in Point errors. y = y. Value; // not allowed: y private in Point set. Radius( radius. Value ); } // set radius public void set. Radius( double radius. Value ) { radius = ( radius. Value < 0. 0 ? 0. 0 : radius. Value ); } Circle 2. java Line 4 Class Circle 2 extends class Point. Line 5 Maintain private instance variable radius. Lines 17 -18 Attempting to access superclass Point’s private instance variables x and y results in syntax errors. 2003 Prentice Hall, Inc. All rights reserved.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 Outline // calculate and return diameter public double get. Diameter() { return 2 * radius; } 21 // calculate and return circumference public double get. Circumference() { return Math. PI * get. Diameter(); } // calculate and return area public double get. Area() { return Math. PI * radius; } Circle 2. java Line 56 Attempting to access superclass Point’s private instance Attempting to access superclass object Point’s private instance variables x and y results in syntax errors. privatesyntax in Point errors. // return String representation of Circle public String to. String() { // use of x and y not allowed: x and y return "Center = [" + x + ", " + y + "]; Radius = " + radius; } } // end class Circle 2 2003 Prentice Hall, Inc. All rights reserved.
Outline Circle 2. java: 17: x has private access in Point x = x. Value; // not allowed: x private in Point ^ Circle 2. java: 18: y has private access in Point y = y. Value; // not allowed: y private in Point ^ Circle 2. java: 56: x has private access in Point return "Center = [" + x + ", " + y + "]; Radius = " + radius; ^ Circle 2. java: 56: y has private access in Point return "Center = [" + x + ", " + y + "]; Radius = " + radius; ^ 4 errors Attempting to access superclass Point’s private instance variables x and y results in syntax errors. 22 Circle 2. java output Attempting to access superclass Point’s private instance variables x and y results in syntax errors. 2003 Prentice Hall, Inc. All rights reserved.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 // Fig. 9. 9: Point 2. java // Point 2 class declaration represents an x-y coordinate pair. public class Point 2 { protected int x; // x part of protected int y; // y part of Maintain x- and y-coordinates as protected instance variables, accessible to coordinate pair subclasses. coordinate pair // no-argument constructor public Point 2() { // implicit call to Object constructor occurs here } // constructor public Point 2( int x. Value, int y. Value ) { // implicit call to Object constructor occurs here x = x. Value; // no need for validation y = y. Value; // no need for validation } Outline 23 Point 2. java Lines 5 -6 Maintain x- and ycoordinates as protected instance variables, accessible to subclasses. // set x in coordinate pair public void set. X( int x. Value ) { x = x. Value; // no need for validation } 2003 Prentice Hall, Inc. All rights reserved.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 // return x from coordinate pair public int get. X() { return x; } Outline // set y in coordinate pair public void set. Y( int y. Value ) { y = y. Value; // no need for validation } // return y from coordinate pair public int get. Y() { return y; } Point 2. java // return String representation of Point 2 object public String to. String() { return "[" + x + ", " + y + "]"; } } // end class Point 2 2003 Prentice Hall, Inc. All rights reserved. 24
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 // Fig. 9. 10: Circle 3. java Circle 3 inherits from // Circle 3 class inherits from Point 2 and has Class access to Point 2 // protected members x and y. class Point 2. Maintain private instance variables radius. public class Circle 3 extends Point 2 { private double radius; // Circle 3's radius // no-argument constructor public Circle 3() { // implicit call to Point 2 constructor occurs here } Implicitly calls superclass’s default constructor. // constructor public Circle 3( int x. Value, int Modify y. Value, inherited double radius. Value ) instance { variables x and y, declared // implicit call to Point 2 constructor occurs here protected in superclass x = x. Value; // no need for validation Point 2. y = y. Value; // no need for validation set. Radius( radius. Value ); } // set radius public void set. Radius( double radius. Value ) { radius = ( radius. Value < 0. 0 ? 0. 0 : radius. Value ); } Outline Circle 3. java Line 5 Class Circle 3 inherits from class Point 2. Line 6 Maintain private instance variables radius. Lines 11 and 17 Implicitly call superclass’s default constructor. Lines 18 -19 Modify inherited instance variables x and y, declared protected in superclass Point 2. 2003 Prentice Hall, Inc. All rights reserved. 25
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 // return radius public double get. Radius() { return radius; } Outline // calculate and return diameter public double get. Diameter() { return 2 * radius; } // calculate and return circumference public double get. Circumference() { return Math. PI * get. Diameter(); } // calculate and return area public double get. Area() { return Math. PI * radius; } Circle 3. java Line 56 Access inherited instance variables x and y, declared protected in superclass Point 2. // return String representation of Circle 3 object public String to. String() { return "Center = [" + x + ", " + y + "]; Radius = " + radius; } } // end class Circle 3 2003 Prentice Hall, Inc. All rights reserved. 26
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 // Fig. 9. 11: Circle. Test 3. java // Testing class Circle 3. import java. text. Decimal. Format; import javax. swing. JOption. Pane; Outline 27 Circletest 3. java Line 11 Create Circle 3 object. public static void main( String[] args ) Lines 14 -15 { Use inherited get methods // instantiate Circle object to access inherited Circle 3 circle = new Circle 3( 37, 43, 2. 5 ); protected instance Use inherited get methods to Use Circle 3 get method to variables x and y. // get Circle 3's initial x-y coordinates and radius access inherited protected access private instance variables and y. y. String output = "X coordinate is " + circle. get. X() + instance variables xxand Line 16 variables. "n. Y coordinate is " + circle. get. Y() + Use Circle 3 get "n. Radius is " + circle. get. Radius(); method to access private instance circle. set. X( 35 ); // set new x-coordinate variables. circle. set. Y( 20 ); // set new y-coordinate circle. set. Radius( 4. 25 ); // set new radius Use inherited set methods to Lines 18 -19 Use inherited set methods modify inherited to modify inherited // get String representation of new circleprotected value data x and y. Use Circle 3 set method to protected data x and y. output += "nn. The new location and radius of circle aren" + Line 20 modify private data circle. to. String(); Use Circle 3 set radius. method to modify private data radius. public class Circle. Test 3 { 2003 Prentice Hall, Inc. All rights reserved.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 // format floating-point values with 2 digits of precision Decimal. Format two. Digits = new Decimal. Format( "0. 00" ); Outline 28 // get Circle's diameter output += "n. Diameter is " + two. Digits. format( circle. get. Diameter() ); // get Circle's circumference output += "n. Circumference is " + two. Digits. format( circle. get. Circumference() ); // get Circle's area output += "n. Area is " + two. Digits. format( circle. get. Area() ); JOption. Pane. show. Message. Dialog( null, output ); // display output Circletest 3. jav a System. exit( 0 ); } // end method main } // end class Circle. Test 3 2003 Prentice Hall, Inc. All rights reserved.
9. 4 Relationship between Superclasses and Subclasses (Cont. ) • Using protected instance variables – Advantages • subclasses can modify values directly • Slight increase in performance – Avoid set/get function call overhead – Disadvantages • No validity checking – subclass can assign illegal value • Implementation dependent – subclass methods more likely dependent on superclass implementation – superclass implementation changes may result in subclass modifications • Fragile (brittle) software 2003 Prentice Hall, Inc. All rights reserved. 29
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 // Fig. 9. 12: Point 3. java // Point class declaration represents an x-y coordinate pair. Better software-engineering practice: private over protected coordinate pair when possible. Outline 30 public class Point 3 { private int x; // x part of private int y; // y part of coordinate pair // no-argument constructor public Point 3() { // implicit call to Object constructor occurs here } // constructor public Point 3( int x. Value, int y. Value ) { // implicit call to Object constructor occurs here x = x. Value; // no need for validation y = y. Value; // no need for validation } Point 3. java Lines 5 -6 Better softwareengineering practice: private over protected when possible. // set x in coordinate pair public void set. X( int x. Value ) { x = x. Value; // no need for validation } 2003 Prentice Hall, Inc. All rights reserved.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 // return x from coordinate pair public int get. X() { return x; } Outline // set y in coordinate pair public void set. Y( int y. Value ) { y = y. Value; // no need for validation } // return y from coordinate pair public int get. Y() { return y; } Point 3. java Line 49 Invoke public methods to access private instance variables. object variables. // return String representation of Point 3 public String to. String() { return "[" + get. X() + ", " + get. Y() + "]"; } } // end class Point 3 2003 Prentice Hall, Inc. All rights reserved. 31
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 // Fig. 9. 13: Circle 4. java Class Circle 4 // Circle 4 class inherits from Point 3 and accesses Point 3's class Point 3. // private x and y via Point 3's public methods. public class Circle 4 extends Point 3 { private double radius; inherits from Outline Maintain private instance variable radius. // Circle 4's radius // no-argument constructor public Circle 4() { // implicit call to Point 3 constructor occurs here } // constructor public Circle 4( int x. Value, int y. Value, double radius. Value ) { super( x. Value, y. Value ); // call Point 3 constructor explicitly set. Radius( radius. Value ); } Circle 4. java Line 5 Class Circle 4 inherits from class Point 3. Line 7 Maintain private instance variable radius. // set radius public void set. Radius( double radius. Value ) { radius = ( radius. Value < 0. 0 ? 0. 0 : radius. Value ); } 2003 Prentice Hall, Inc. All rights reserved. 32
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 // return radius public double get. Radius() { return radius; } Outline // calculate and return diameter public double get. Diameter() { return 2 * get. Radius(); } // calculate and return circumference public double get. Circumference() { return Math. PI * get. Diameter(); } 33 Circle 4. java Invoke method get. Radius rather than directly accessing instance variable radius. // calculate and return area public double get. Area() { return Math. PI * get. Radius(); Redefine } class Point 3’s method to. String. // return String representation of Circle 4 object public String to. String() { return "Center = " + super. to. String() + "; Radius = " + get. Radius(); } Line 37, 49 and 55 Invoke method get. Radius rather than directly accessing instance variable radius. Lines 53 -56 Redefine class Point 3’s method to. String. } // end class Circle 4 2003 Prentice Hall, Inc. All rights reserved.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 // Fig. 9. 14: Circle. Test 4. java // Testing class Circle 4. import java. text. Decimal. Format; import javax. swing. JOption. Pane; Outline 34 Circletest 4. java Create Circle 4 object. Line 11 public static void main( String[] args ) Create Circle 4 object. { Lines 14 and 15 Use inherited get methods // instantiate Circle object Use inherited get methods to to access inherited Circle 4 circle = new Circle 4( 37, 43, 2. 5 ); access inheritedprivate instance variables andtoy. instance variables andxy. // get Circle 4's initial x-y coordinates and radius Use Circle 4 getxmethod Line 16 String output = "X coordinate is " + circle. get. X() + access private instance Use Circle 4 get "n. Y coordinate is " + circle. get. Y() + variable radius. method to access "n. Radius is " + circle. get. Radius(); private instance variable radius. circle. set. X( 35 ); // set new x-coordinate Lines 18 -19 circle. set. Y( 20 ); // set new y-coordinate Use inherited seta methods to Use inherited seta circle. set. Radius( 4. 25 ); // set new radius modify inherited private methods to modify instance variables x and y. to inherited private // get String representation of new circle value Use Circle 4 set method instance variables x and y. output += "nn. The new location and radius of circle aren" + modify private instance Line 20 circle. to. String(); variable radius. Use Circle 4 set method to modify private instance variable radius. public class Circle. Test 4 { 2003 Prentice Hall, Inc. All rights reserved.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 // format floating-point values with 2 digits of precision Decimal. Format two. Digits = new Decimal. Format( "0. 00" ); Outline 35 // get Circle's diameter output += "n. Diameter is " + two. Digits. format( circle. get. Diameter() ); // get Circle's circumference output += "n. Circumference is " + two. Digits. format( circle. get. Circumference() ); // get Circle's area output += "n. Area is " + two. Digits. format( circle. get. Area() ); JOption. Pane. show. Message. Dialog( null, output ); // display output Circletest 4. jav a System. exit( 0 ); } // end main } // end class Circle. Test 4 2003 Prentice Hall, Inc. All rights reserved.
9. 5 Case Study: Three-Level Inheritance Hierarchy • Three level point/circle/cylinder hierarchy – Point • x-y coordinate pair – Circle • x-y coordinate pair • Radius – Cylinder • x-y coordinate pair • Radius • Height 2003 Prentice Hall, Inc. All rights reserved. 36
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 // Fig. 9. 15: Cylinder. java // Cylinder class inherits from Circle 4. Maintain private instance variable height. Outline public class Cylinder extends Circle 4 { private double height; // Cylinder's height Class Cylinder extends // no-argument constructor class Circle 4. public Cylinder() { // implicit call to Circle 4 constructor occurs here } // constructor public Cylinder( int x. Value, int y. Value, double radius. Value, double height. Value ) { super( x. Value, y. Value, radius. Value ); // call Circle 4 constructor set. Height( height. Value ); } // set Cylinder's height public void set. Height( double height. Value ) { height = ( height. Value < 0. 0 ? 0. 0 : height. Value ); } Cylinder. java Line 4 Class Cylinder extends class Circle 4. Line 5 Maintain private instance variable height. 2003 Prentice Hall, Inc. All rights reserved. 37
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 // get Cylinder's height public double get. Height() { return height; } Outline Redefine superclass Circle 4’s method Invoke superclass calculate Cylinder area get. Area to return Circle 4’s get. Area Cylinder surface area. method using keyword super. 38 Cylinder. java // override Circle 4 method get. Area to public double get. Area() { return 2 * super. get. Area() + get. Circumference() * get. Height(); } Line 34 and 42 Redefine superclass Circle 4’s method get. Area to return Cylinder surface area. // calculate Cylinder volume public double get. Volume() { return super. get. Area() * get. Height(); } Line 36 Invoke superclass Circle 4’s get. Area method using keyword super. Redefine class Circle 4’s method to. String. Lines 46 -49 Cylinder Invoke objectsuperclass Redefine class Circle 4’s to. String Circle 4’s method using keyword super. // return String representation of public String to. String() { return super. to. String() + "; Height = " + get. Height(); } } // end class Cylinder to. String. Line 48 Invoke superclass Circle 4’s to. String method using keyword super. 2003 Prentice Hall, Inc. All rights reserved.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 // Fig. 9. 16: Cylinder. Test. java // Testing class Cylinder. import java. text. Decimal. Format; import javax. swing. JOption. Pane; Outline 39 Cylinder. Test. java public class Cylinder. Test { Lines 14 and 15 Invoke indirectly inherited Point 3 get public static void main( String[] args ) methods. { Line 16 // create Cylinder object directly inherited Cylinder cylinder = new Cylinder( 12, 23, 2. 5, 5. 7 ); Invoke indirectly. Invoke Circle 4 get method. Point 3 get methods. Invoke Cylinder Line 16 get method. // get Cylinder's initial x-y coordinates, radius and height Invoke directly inherited Invoke Cylinder get String output = "X coordinate is " + cylinder. get. X() Circle 4 get+method. "n. Y coordinate is " + cylinder. get. Y() + "n. Radius is " + Lines 18 -19 cylinder. get. Radius() + "n. Height is " + cylinder. get. Height(); Invoke indirectly inherited Point 3 set cylinder. set. X( 35 ); // set new x-coordinate Invoke indirectly inherited methods. cylinder. set. Y( 20 ); // set new y-coordinate Point 3 set methods. Line 20 cylinder. set. Radius( 4. 25 ); // set new radius Invoke directly inherited cylinder. set. Height( 10. 75 ); // set new Invoke height directly inherited Circle 4 set method. Invoke Cylinder set Line 21 // get String representation of new cylinder value Invoke Cylinder set method. output += method. "nn. The new location, radius and height of cylinder aren" + Line 26 cylinder. to. String(); Invoke overridden to. String method. 2003 Prentice Hall, Inc. All rights reserved.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 // format floating-point values with 2 digits of precision Decimal. Format two. Digits = new Decimal. Format( "0. 00" ); Outline 40 // get Cylinder's diameter output += "nn. Diameter is " + two. Digits. format( cylinder. get. Diameter() ); // get Cylinder's circumference output += "n. Circumference is " + two. Digits. format( cylinder. get. Circumference() ); // get Cylinder's area output += "n. Area is " + two. Digits. format( cylinder. get. Area() ); Cylinder. Test. ja va Invoke overridden get. Area // get Cylinder's volume output += "n. Volume is " + two. Digits. format( cylinder. get. Volume() ); method. Line 40 JOption. Pane. show. Message. Dialog( null, output ); // display output Invoke overridden get. Area method. System. exit( 0 ); } // end main } // end class Cylinder. Test 2003 Prentice Hall, Inc. All rights reserved.
9. 6 Constructors and Finalizers in Subclasses • Instantiating subclass object – Chain of constructor calls • subclass constructor invokes superclass constructor – Implicitly or explicitly • Base of inheritance hierarchy – Last constructor called in chain is Object’s constructor – Original subclass constructor’s body finishes executing last – Example: Point 3/Circle 4/Cylinder hierarchy • Point 3 constructor called second last (last is Object constructor) • Point 3 constructor’s body finishes execution second (first is Object constructor’s body) 2003 Prentice Hall, Inc. All rights reserved. 41
9. 6 Constructors and Destructors in Derived Classes • Garbage collecting subclass object – Chain of finalize method calls • Reverse order of constructor chain • Finalizer of subclass called first • Finalizer of next superclass up hierarchy next – Continue up hierarchy until final superreached • After final superclass (Object) finalizer, object removed from memory 2003 Prentice Hall, Inc. All rights reserved. 42
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 // Fig. 9. 17: Point. java // Point class declaration represents an x-y coordinate pair. Outline public class Point { private int x; // x part of coordinate pair private int y; // y part of coordinate pair // no-argument constructor public Point() { // implicit call to Object constructor occurs here System. out. println( "Point no-argument constructor: " + this ); } // constructor public Point( int x. Value, int y. Value ) { // implicit call to Object constructor occurs here x = x. Value; // no need for validation y = y. Value; // no need for validation System. out. println( "Point constructor: " + this ); Point. java Lines 12, 22 and 28 Constructor and finalizer Constructor and output messages to finalizer output demonstrate method call order. messages to demonstrate method call order. } // finalizer protected void finalize() { System. out. println( "Point finalizer: " + this ); } 2003 Prentice Hall, Inc. All rights reserved. 43
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 // set x in coordinate pair public void set. X( int x. Value ) { x = x. Value; // no need for validation } Outline // return x from coordinate pair public int get. X() { return x; } // set y in coordinate pair public void set. Y( int y. Value ) { y = y. Value; // no need for validation } Point. java // return y from coordinate pair public int get. Y() { return y; } // return String representation of Point 4 object public String to. String() { return "[" + get. X() + ", " + get. Y() + "]"; } } // end class Point 2003 Prentice Hall, Inc. All rights reserved. 44
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 // Fig. 9. 18: Circle. java // Circle 5 class declaration. Outline public class Circle extends Point { private double radius; // Circle's radius // no-argument constructor public Circle() { // implicit call to Point constructor occurs here System. out. println( "Circle no-argument constructor: " + this ); } // constructor public Circle( int x. Value, int y. Value, double radius. Value ) { super( x. Value, y. Value ); // call Point constructor set. Radius( radius. Value ); System. out. println( "Circle constructor: " + this ); } Circle. java Constructor and finalizer output messages to Lines 12, 21 and 29 demonstrate method call order. Constructor and finalizer output messages to demonstrate method call order. // finalizer protected void finalize() { System. out. println( "Circle finalizer: " + this ); super. finalize(); // call superclass finalize method } 2003 Prentice Hall, Inc. All rights reserved. 45
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 // set radius public void set. Radius( double radius. Value ) { radius = ( radius. Value < 0. 0 ? 0. 0 : radius. Value ); } Outline // return radius public double get. Radius() { return radius; } // calculate and return diameter public double get. Diameter() { return 2 * get. Radius(); } Circle. java // calculate and return circumference public double get. Circumference() { return Math. PI * get. Diameter(); } 2003 Prentice Hall, Inc. All rights reserved. 46
55 56 57 58 59 60 61 62 63 64 65 66 67 68 // calculate and return area public double get. Area() { return Math. PI * get. Radius(); } Outline // return String representation of Circle 5 object public String to. String() { return "Center = " + super. to. String() + "; Radius = " + get. Radius(); } } // end class Circle. java 2003 Prentice Hall, Inc. All rights reserved. 47
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 // Fig. 9. 19: Constructor. Finalizer. Test. java // Display order in which superclass and subclass // constructors and finalizers are called. Outline 48 public class Constructor. Finalizer. Test { public static void main( String args[] ) { Point point; Circle circle 1, circle 2; Constructor. Fina Point object goes in and out lizer. Test. java of scope immediately. point = new Point( 11, 22 ); System. out. println(); circle 1 = new Circle( 72, 29, 4. 5 ); System. out. println(); circle 2 = new Circle( 5, 7, 10. 67 ); point = null; circle 1 = null; circle 2 = null; // mark for garbage collection System. out. println(); Line 12 Point object goes in Instantiate two Circle and outorder of scope objects to demonstrate immediately. of subclass and superclass constructor/finalizer method Lines 15 and 18 calls. Instantiate two Circle objects to demonstrate order of subclass and superclass constructor/finalizer method calls. 2003 Prentice Hall, Inc. All rights reserved.
26 27 28 29 30 System. gc(); // call the garbage collector Outline 49 } // end main } // end class Constructor. Finalizer. Test Point constructor: [11, 22] Point constructor: Center = [72, 29]; Radius = 0. 0 Circle constructor: Center = [72, 29]; Radius = 4. 5 Point constructor: Center = [5, 7]; Radius = 0. 0 Circle constructor: Center = [5, 7]; Radius = 10. 67 Point finalizer: [11, 22] Circle finalizer: Center = [72, 29]; Radius = 4. 5 Point finalizer: Center = [72, 29]; Radius = 4. 5 Circle finalizer: Center = [5, 7]; Radius = 10. 67 Point finalizer: Center = [5, 7]; Radius = 10. 67 Subclass Circle constructor body executes after superclass Point 4’s constructor finishes execution. Finalizer for Circle object called in reverse order of constructors. Constructor. Fina lizer. Test. java 2003 Prentice Hall, Inc. All rights reserved.
50 9. 9 Software Engineering with Inheritance • Customizing existing software – Inherit from existing classes • Include additional members • Redefine superclass members • No direct access to superclass’s source code – Link to object code – Independent software vendors (ISVs) • Develop proprietary code for sale/license – Available in object-code format • Users derive new classes – Without accessing ISV proprietary source code 2003 Prentice Hall, Inc. All rights reserved.
- Slides: 50