Home | Résumé | Courses | Contact | Useful Links | Favorite Links | USC - Homepage


Computer Science 1 - (CPTR151) - Lectures  


 Return to Courses | Course Content  



Computer Science 1 - (CPTR151)

by David Siguelnitzky, MIS; MTE

Lecture 9 - Version 1.2.1

Object-Oriented Programming: Inheritance



Lecture Outline

9.1 Introduction
9.2 Superclasses and Subclasses
9.3 Relationship between Superclasses and Subclasses


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

    • Class hierarchy
        – Direct superclass
            • Inherited explicitly (one level up hierarchy)
        – Indirect superclass
            • Inherited two or more levels up hierarchy

    • Abstraction
        – Focus on commonalities among objects in system
    • “is-a” vs. “has-a”
        – “is-a”
            • subclass object treated as superclass object
            • Example: Car is a vehicle
                – Vehicle properties/behaviors also car properties/behaviors
        – “has-a”
            • Object contains one or more objects of other classes as members
            • Example: Car has a steering wheel


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

    • Inheritance hierarchy
        – Inheritance relationships: tree-like hierarchy structure
        – Each class becomes
    • superclass
        – If supply data/behaviors to other classes
    OR
    • subclass
        – If inherit data/behaviors from other classes

 


Inheritance hierarchy for Shapes


9.3 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


1 // Fig. 9.4: Point.java
2 // Point class declaration represents an x-y coordinate pair.
3
4 public class Point {
5     private int x; // x part of coordinate pair
6     private int y; // y part of coordinate pair
7
8     // no-argument constructor
9     public Point()
10     {
11             // implicit call to Object constructor occurs here
12     }
13
14     // constructor
15     public Point( int xValue, int yValue )
16     {
17             // implicit call to Object constructor occurs here
18             x = xValue;
19             y = yValue;
20     }
21
22     // set x in coordinate pair
23     public void setX( int xValue )
24     {
25             x = xValue;
26     }
27
28     // return x from coordinate pair
29     public int getX()
30     {
31             return x;
32     }
33
34     // set y in coordinate pair
35     public void setY( int yValue )
36     {
37             y = yValue;
38     }
39
40     // return y from coordinate pair
41     public int getY()
42     {
43             return y;
44     }
45
46     // return String representation of Point object
47     public String toString()
48     {
49             return "[" + x + ", " + y + "]";
50     }
51
52 } // end class Point



    Lines 5-6 Maintain x- and y-coordinates as private instance variables.
    Line 11 Implicit call to Object constructor
    Lines 47-50 Override method toString of class Object.

 

1 // Fig. 9.5: PointTest.java
2 // Testing class Point.
3 import javax.swing.JOptionPane;
4
5 public class PointTest {
6
7         public static void main( String[] args )
8         {
9                 Point point = new Point( 72, 115 ); // create Point object
10
11                 // get point coordinates
12                 String output = "X coordinate is " + point.getX() + "\nY coordinate is " + point.getY();
13                
14
15                 point.setX( 10 ); // set x-coordinate
16                 point.setY( 20 ); // set y-coordinate
17
18                // get String representation of new point value
19                 output += "\n\nThe new location of point is " + point;
20
21                 JOptionPane.showMessageDialog( null, output ); // display output
22
23                 System.exit( 0 );
24
25         } // end main
26
27 } // end class PointTest



    Line 9 Instantiate Point object
    Lines 15-16 Change the value of point’s x- and y- coordinates
    Line 19 Implicitly call point’s toString method

 

 


1 // Fig. 9.6: Circle.java
2 // Circle class contains x-y coordinate pair and radius.
3
4 public class Circle {
5         private int x; // x-coordinate of Circle's center
6         private int y; // y-coordinate of Circle's center
7         private double radius; // Circle's radius
8
9         // no-argument constructor
10         public Circle()
11         {
12             // implicit call to Object constructor occurs here
13         }
14
15         // constructor
16         public Circle( int xValue, int yValue, double radiusValue )
17         {
18                 // implicit call to Object constructor occurs here
19                 x = xValue; // no need for validation
20                 y = yValue; // no need for validation
21                 setRadius( radiusValue );
22         }
23
24         // set x in coordinate pair
25         public void setX( int xValue )
26         {
27                 x = xValue; // no need for validation
28         }
29
30         // return x from coordinate pair
31         public int getX()
32         {
33                 return x;
34         }
35
36         // set y in coordinate pair
37         public void setY( int yValue )
38         {
39                 y = yValue; // no need for validation
40         }
41
42         // return y from coordinate pair
43         public int getY()
44         {
45                 return y;
46         }
47
48         // set radius
49         public void setRadius( double radiusValue )
50         {
51                 radius = ( radiusValue < 0.0 ? 0.0 : radiusValue );
52         }
53
54         // return radius
55         public double getRadius()
56         {
57                 return radius;
58         }
59
60         // calculate and return diameter
61         public double getDiameter()
62         {
63                 return 2 * radius;
64         }
65
66         // calculate and return circumference
67         public double getCircumference()
68         {
69                 return Math.PI * getDiameter();
70         }
71
72         // calculate and return area
73         public double getArea()
74         {
75                 return Math.PI * radius * radius;
76         }
77
78         // return String representation of Circle object
79         public String toString()
80         {
81                 return "Center = [" + x + ", " + y + "]; Radius = " + radius;
82         }
83
84 } // end class Circle



    Lines 5-7 Maintain x- and y- coordinates and radius as private instance variables.
    Lines 25-28 Note code similar to Point code.
    Lines 31-47 Note code similar to Point code. Line 51 Ensure non-negative value for radius.


1 // Fig. 9.7: CircleTest.java
2 // Testing class Circle.
3 import java.text.DecimalFormat;
4 import javax.swing.JOptionPane;
5
6 public class CircleTest {
7
8     public static void main( String[] args )
9     {
10             Circle circle = new Circle( 37, 43, 2.5 ); // create Circle object
11
12             // get Circle's initial x-y coordinates and radius
13             String output = "X coordinate is " + circle.getX() +
14             "\nY coordinate is " + circle.getY() + "\nRadius is " + circle.getRadius();
15
16
17             circle.setX( 35 ); // set new x-coordinate
18             circle.setY( 20 ); // set new y-coordinate
19             circle.setRadius( 4.25 ); // set new radius
20
21             // get String representation of new circle value
22             output += "\n\nThe new location and radius of circle are\n" + circle.toString();
23 
24
25             // format floating-point values with 2 digits of precision
26             DecimalFormat twoDigits = new DecimalFormat( "0.00" );
27
28             // get Circle's diameter
29             output += "\nDiameter is " + twoDigits.format( circle.getDiameter() );
30
31
32             // get Circle's circumference
33             output += "\nCircumference is " + twoDigits.format( circle.getCircumference() );
34
35
36             // get Circle's area
37             output += "\nArea is " + twoDigits.format( circle.getArea() );
38
39             JOptionPane.showMessageDialog( null, output ); // display output
40
41             System.exit( 0 );
42
43     } // end main
44
45 } // end class CircleTest



    Line 10 Create Circle object
    Lines 17-19 Use set methods to modify private instance variable
    Line 23 Explicitly call circle’s toString method
    Lines 29-37 Use get methods to obtain circle’s diameter, circumference and area.

 



 

1 // Fig. 9.8: Circle2.java
2 // Circle2 class inherits from Point.
3
4 public class Circle2 extends Point {
5     private double radius; // Circle2's radius
6
7     // no-argument constructor
8     public Circle2()
9     {
10             // implicit call to Point constructor occurs here
11     }
12
13     // constructor
14     public Circle2( int xValue, int yValue, double radiusValue )
15     {
16             // implicit call to Point constructor occurs here
17             x = xValue; // not allowed: x private in Point
18             y = yValue; // not allowed: y private in Point
19             setRadius( radiusValue );
20     }
21
22     // set radius
23     public void setRadius( double radiusValue )
24     {
25             radius = ( radiusValue < 0.0 ? 0.0 : radiusValue );
26     }
27
34     // calculate and return diameter
35     public double getDiameter()
36     {
37             return 2 * radius;
38     }
39
40     // calculate and return circumference
41     public double getCircumference()
42     {
43             return Math.PI * getDiameter();
44     }
45
46     // calculate and return area
47     public double getArea()
48     {
49             return Math.PI * radius * radius;
50     }
51
52     // return String representation of Circle object
53     public String toString()
54     {
55             // use of x and y not allowed: x and y private in Point
56             return "Center = [" + x + ", " + y + "]; Radius = " + radius;
57     }
58
59 } // end class Circle2



    Line 4 Class Circle2 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.
    Line 56 Attempting to access superclass Point’s private instance variables x and y results in syntax errors.

 


Attempting to access superclass Point’s private instance variables x and y results in syntax errors.


1 // Fig. 9.9: Point2.java
2 // Point2 class declaration represents an x-y coordinate pair.
3
4 public class Point2 {
5     protected int x;     // x part of coordinate pair
6     protected int y;     // y part of coordinate pair
7
8     // no-argument constructor
9     public Point2()
10     {
11             // implicit call to Object constructor occurs here
12     }
13
14     // constructor
15     public Point2( int xValue, int yValue )
16     {
17             // implicit call to Object constructor occurs here
18             x = xValue; // no need for validation
19             y = yValue; // no need for validation
20     }
21
22     // set x in coordinate pair
23     public void setX( int xValue )
24     {
25             x = xValue;     // no need for validation
26     }
27
28     // return x from coordinate pair
29     public int getX()
30     {
31             return x;
32     }
33
34     // set y in coordinate pair
35     public void setY( int yValue )
36     {
37             y = yValue; // no need for validation
38     }
39
40     // return y from coordinate pair
41     public int getY()
42     {
43             return y;
44     }
45
46     // return String representation of Point2 object
47     public String toString()
48     {
49             return "[" + x + ", " + y + "]";
50     }
51
52 } // end class Point2



    Lines 5-6 Maintain x- and y-coordinates as protected instance variables, accessible to subclasses.
 



1 // Fig. 9.9: Point2.java
2 // Point2 class declaration represents an x-y coordinate pair.
3
4 public class Point2 {
5     protected int x; // x part of coordinate pair
6     protected int y; // y part of coordinate pair
7
8     // no-argument constructor
9     public Point2()
10     {
11             // implicit call to Object constructor occurs here
12     }
13
14     // constructor
15     public Point2( int xValue, int yValue )
16     {
17             // implicit call to Object constructor occurs here
18             x = xValue; // no need for validation
19             y = yValue; // no need for validation
20     }
21
22     // set x in coordinate pair
23     public void setX( int xValue )
24     {
25             x = xValue; // no need for validation
26     }
27
28     // return x from coordinate pair
29     public int getX()
30     {
31             return x;
32     }
33
34     // set y in coordinate pair
35     public void setY( int yValue )
36     {
37             y = yValue; // no need for validation
38     }
39
40     // return y from coordinate pair
41     public int getY()
42     {
43             return y;
44     }
45
46     // return String representation of Point2 object
47     public String toString()
48     {
49             return "[" + x + ", " + y + "]";
50     }
51
52 } // end class Point2

    Lines 5-6 Maintain x- and y-coordinates as protected instance variables, accessible to subclasses.


1 // Fig. 9.10: Circle3.java
2 // Circle3 class inherits from Point2 and has access to Point2
3 // protected members x and y.
4
5 public class Circle3 extends Point2 {
6     private double radius; // Circle3's radius
7
8     // no-argument constructor
9     public Circle3()
10     {
11             // implicit call to Point2 constructor occurs here
12     }
13
14     // constructor
15     public Circle3( int xValue, int yValue, double radiusValue )
16     {
17             // implicit call to Point2 constructor occurs here
18             x = xValue; // no need for validation
19             y = yValue; // no need for validation
20             setRadius( radiusValue );
21     }
22
23     // set radius
24     public void setRadius( double radiusValue )
25     {
26             radius = ( radiusValue < 0.0 ? 0.0 : radiusValue );
27     }
28
29     // return radius
30     public double getRadius()
31     {
32             return radius;
33     }
34
35     // calculate and return diameter
36     public double getDiameter()
37     {
38             return 2 * radius;
39     }
40
41     // calculate and return circumference
42     public double getCircumference()
43     {
44             return Math.PI * getDiameter();
45     }
46
47     // calculate and return area
48     public double getArea()
49     {
50             return Math.PI * radius * radius;
51     }
52
53     // return String representation of Circle3 object
54     public String toString()
55     {
56             return "Center = [" + x + ", " + y + "]; Radius = " + radius;
57     }
58
59 } // end class Circle3



    Line 5 Class Circle3 inherits from class Point2.
    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 Point2.
    Line 56 Access inherited instance variables x and y, declared protected in superclass Point2.

 


1 // Fig. 9.11: CircleTest3.java
2 // Testing class Circle3.
3 import java.text.DecimalFormat;
4 import javax.swing.JOptionPane;
5
6 public class CircleTest3 {
7
8     public static void main( String[] args )
9     {
10             // instantiate Circle object
11             Circle3 circle = new Circle3( 37, 43, 2.5 );
12
13             // get Circle3's initial x-y coordinates and radius
14             String output = "X coordinate is " + circle.getX() +
15             "\nY coordinate is " + circle.getY() + "\nRadius is " + circle.getRadius();
16
17
18             circle.setX( 35 ); // set new x-coordinate
19             circle.setY( 20 ); // set new y-coordinate
20             circle.setRadius( 4.25 ); // set new radius
21
22             // get String representation of new circle value
23             output += "\n\nThe new location and radius of circle are\n" + circle.toString();
24
25
26             // format floating-point values with 2 digits of precision
27             DecimalFormat twoDigits = new DecimalFormat( "0.00" );
28
29             // get Circle's diameter
30             output += "\nDiameter is " + twoDigits.format( circle.getDiameter() );
31
32
33             // get Circle's circumference
34             output += "\nCircumference is " +
35             twoDigits.format( circle.getCircumference() );
36
37             // get Circle's area
38             output += "\nArea is " + twoDigits.format( circle.getArea() );
39
40             JOptionPane.showMessageDialog( null, output ); // display output
41
42             System.exit( 0 );
43
44     } // end method main
45
46 } // end class CircleTest3



    Line 11 Create Circle3 object.
    Lines 14-15 Use inherited get methods to access inherited protected instance variables x and y. variables x and y.
    Line 16 Use Circle3 get method to access private instance variables.
    Lines 18-19 Use inherited set methods to modify inherited protected data x and y.
    Line 20 Use Circle3 set method to modify private data radius.

 

 


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


                    Source: Java How to Program – 5th Edition – Deitel & Deitel



 Return to Courses | Course Content  


 Home | Résumé | Courses | Contact | Useful Links | Favorite Links | USC - Homepage