Download November 13, 2003 (3 level inheritance)

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
1
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.
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 Circle4.
Maintain private instance
variable height.
public class Cylinder extends Circle4 {
private double height; // Cylinder's height
Class Cylinder extends
// no-argument constructor
class Circle4.
public Cylinder()
{
// implicit call to Circle4 constructor occurs here
}
// constructor
public Cylinder( int xValue, int yValue, double radiusValue,
double heightValue )
{
super( xValue, yValue, radiusValue ); // call Circle4 constructor
setHeight( heightValue );
}
2
Outline
Cylinder.java
Line 4
Class Cylinder
extends class
Circle4.
Line 5
Maintain private
instance variable
height.
// set Cylinder's height
public void setHeight( double heightValue )
{
height = ( heightValue < 0.0 ? 0.0 : heightValue );
}
 2003 Prentice Hall, Inc.
All rights reserved.
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 getHeight()
{
return height;
}
3
Outline
Redefine superclass
Circle4’s method
Invoke
superclass
calculate
Cylinder
area
getArea
to return
Circle4’s getArea
Cylinder surface area.
method using keyword super.
Cylinder.java
// override Circle4 method getArea to
public double getArea()
{
return 2 * super.getArea() + getCircumference() * getHeight();
}
Line 34 and 42
Redefine superclass
Circle4’s method
getArea to return
Cylinder surface area.
// calculate Cylinder volume
public double getVolume()
{
return super.getArea() * getHeight();
}
Line 36
Invoke superclass
Circle4’s getArea
method using keyword
super.
Redefine class Circle4’s
method toString.
Cylinder Invoke
objectsuperclass
Circle4’s toString
method using keyword super.
// return String representation of
public String toString()
{
return super.toString() + "; Height = " + getHeight();
}
} // end class Cylinder
Lines 46-49
Redefine class
Circle4’s method
toString.
Line 48
Invoke superclass
Circle4’s toString
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: CylinderTest.java
// Testing class Cylinder.
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
4
Outline
CylinderTest.java
Lines 14 and 15
public class CylinderTest {
Invoke indirectly
inherited Point3 get
public static void main( String[] args )
methods.
{
Line 16
// create Cylinder object
directly inherited
inherited
Cylinder cylinder = new Cylinder( 12, 23, 2.5, 5.7 ); Invoke indirectlyInvoke
Circle4 get method.
Point3 get methods.
Line 16get method.
Invoke Cylinder
// get Cylinder's initial x-y coordinates,
radius
and height
Invoke
directly
inherited
Invoke Cylinder get
String output = "X coordinate is " + cylinder.getX()
Circle4 get+method.
method.
"\nY coordinate is " + cylinder.getY() + "\nRadius is " +
Lines 18-19
cylinder.getRadius() + "\nHeight is " + cylinder.getHeight();
Invoke indirectly
inherited Point3 set
cylinder.setX( 35 );
// set new x-coordinate
Invoke indirectly inherited
methods.
cylinder.setY( 20 );
// set new y-coordinate
Line 20
Point3 set methods.
cylinder.setRadius( 4.25 );
// set new radius
Invoke directly inherited
cylinder.setHeight( 10.75 ); // set new Invoke
height directly inherited
Circle4 set method.
Circle4 set method.
Line 21
Invoke Cylinder set
// get String representation of new cylinder value
Invoke Cylinder set
method.
output +=
method.
"\n\nThe new location, radius and height of cylinder are\n" +
Line 26
cylinder.toString();
Invoke overridden
toString method.
Invoke overridden
method.
toString method.
 2003 Prentice Hall, Inc.
All rights reserved.
5
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
DecimalFormat twoDigits = new DecimalFormat( "0.00" );
Outline
// get Cylinder's diameter
output += "\n\nDiameter is " +
twoDigits.format( cylinder.getDiameter() );
CylinderTest.ja
va
// get Cylinder's circumference
output += "\nCircumference is " +
twoDigits.format( cylinder.getCircumference() );
Line 40
Invoke overridden
getArea method.
// get Cylinder's area
output += "\nArea is " + twoDigits.format( cylinder.getArea() );
Invoke overridden
// get Cylinder's volume
output += "\nVolume is " + twoDigits.format( cylinder.getVolume()
);
method.
getArea
JOptionPane.showMessageDialog( null, output ); // display output
System.exit( 0 );
} // end main
} // end class CylinderTest
 2003 Prentice Hall, Inc.
All rights reserved.
6
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: Point3/Circle4/Cylinder hierarchy
• Point3 constructor called second last (last is
Object constructor)
• Point3 constructor’s body finishes execution
second (first is Object constructor’s body)
 2003 Prentice Hall, Inc. All rights reserved.
7
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.
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.
8
Outline
public class Point {
private int x; // x part of coordinate pair
private int y; // y part of coordinate pair
Point.java
Lines 12, 22 and 28
Constructor and
finalizer output
messages to
demonstrate method
call order.
Constructor and finalizer
output messages to
demonstrate method call order.
// no-argument constructor
public Point()
{
// implicit call to Object constructor occurs here
System.out.println( "Point no-argument constructor: " + this );
}
// constructor
public Point( int xValue, int yValue )
{
// implicit call to Object constructor occurs here
x = xValue; // no need for validation
y = yValue; // no need for validation
System.out.println( "Point constructor: " + this );
}
// finalizer
protected void finalize()
{
System.out.println( "Point finalizer: " + this );
}
 2003 Prentice Hall, Inc.
All rights reserved.
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 setX( int xValue )
{
x = xValue; // no need for validation
}
9
Outline
Point.java
// return x from coordinate pair
public int getX()
{
return x;
}
// set y in coordinate pair
public void setY( int yValue )
{
y = yValue; // no need for validation
}
// return y from coordinate pair
public int getY()
{
return y;
}
// return String representation of Point4 object
public String toString()
{
return "[" + getX() + ", " + getY() + "]";
}
} // end class Point
 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
30
31
// Fig. 9.18: Circle.java
// Circle5 class declaration.
10
Outline
public class Circle extends Point {
private double radius;
Circle.java
// 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 xValue, int yValue, double radiusValue )
{
super( xValue, yValue ); // call Point constructor
setRadius( radiusValue );
Lines 12, 21 and 29
Constructor and
finalizer output
messages to
demonstrate method
Constructor and finalizer
call order.
output messages to
demonstrate method call order.
System.out.println( "Circle constructor: " + this );
}
// finalizer
protected void finalize()
{
System.out.println( "Circle finalizer: " + this );
super.finalize();
// call superclass finalize method
}
 2003 Prentice Hall, Inc.
All rights reserved.
11
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 setRadius( double radiusValue )
{
radius = ( radiusValue < 0.0 ? 0.0 : radiusValue );
}
Outline
Circle.java
// return radius
public double getRadius()
{
return radius;
}
// calculate and return diameter
public double getDiameter()
{
return 2 * getRadius();
}
// calculate and return circumference
public double getCircumference()
{
return Math.PI * getDiameter();
}
 2003 Prentice Hall, Inc.
All rights reserved.
12
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// calculate and return area
public double getArea()
{
return Math.PI * getRadius() * getRadius();
}
Outline
Circle.java
// return String representation of Circle5 object
public String toString()
{
return "Center = " + super.toString() + "; Radius = " + getRadius();
}
} // end class Circle
 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
// Fig. 9.19: ConstructorFinalizerTest.java
// Display order in which superclass and subclass
// constructors and finalizers are called.
ConstructorFina
lizerTest.java
public class ConstructorFinalizerTest {
public static void main( String args[] )
{
Point point;
Circle circle1, circle2;
point = new Point( 11, 22 );
System.out.println();
circle1 = new Circle( 72, 29, 4.5 );
Line 12
Point object goes in and out
Point object goes in
of scope immediately.
and out of scope
immediately.
Instantiate two Circle
objects to demonstrate
order
Lines 15
and 18
of subclass and superclass
Instantiate two
constructor/finalizer
method
Circle
objects to
calls.
demonstrate order of
System.out.println();
circle2 = new Circle( 5, 7, 10.67 );
point = null;
circle1 = null;
circle2 = null;
Outline
// mark for garbage collection
// mark for garbage collection
// mark for garbage collection
subclass and
superclass
constructor/finalizer
method calls.
System.out.println();
 2003 Prentice Hall, Inc.
All rights reserved.
14
26
27
28
29
30
System.gc();
// call the garbage collector
Outline
} // end main
} // end class ConstructorFinalizerTest
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
ConstructorFina
lizerTest.java
Subclass Circle constructor
body executes after superclass
Point4’s constructor
finishes execution.
Finalizer for Circle object
called in reverse order of
constructors.
 2003 Prentice Hall, Inc.
All rights reserved.
15
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.
Related documents