9.7. Polymorphism, Dynamic Binding, and Generic Programming

 
[Page 302 ( continued )]

9.2. Superclasses and Subclasses

In Java terminology, a class C1 extended from another class C2 is called a subclass , and C2 is called a superclass . A superclass is also referred to as a supertype , a parent class, or a base class , and a subclass as a subtype , a child class , an extended class , or a derived class . A subclass inherits accessible data fields and methods from its superclass, and may also add new data fields and methods .

Consider geometric objects. Suppose you want to design the classes to model geometric objects like circles and rectangles. Geometric objects have many common properties and behaviors. They can be drawn in a certain color , filled or unfilled. Thus a general class GeometricObject can be used to model all geometric objects. This class contains the properties color and filled and their appropriate get and set methods. Assume that this class also contains the dateCreated property and the getDateCreated() and toString() methods. The toString() method returns a string representation for the object. Since a circle is a special type of geometric object, it shares common properties and methods with other geometric objects. Thus it makes sense to define the Circle class that extends the GeometricObject class. Likewise, Rectangle can also be declared as a subclass of GeometricObject . Figure 9.1 shows the relationship among these classes. An arrow pointing to the superclass is used to denote the inheritance relationship between the two classes involved.

Figure 9.1. The GeometricObject class is the superclass for Circle and Rectangle .
(This item is displayed on page 303 in the print version)

The Circle class inherits all accessible data fields and methods from the GeometricObject class. In addition, it has a new data field, radius , and its associated get and set methods. It also contains the getArea() , getPerimeter() , and getDiameter() methods for returning the area, perimeter, and diameter of the circle.

The Rectangle class inherits all accessible data fields and methods from the GeometricObject class. In addition, it has the data fields width and height , and the associated get and set methods. It also contains the getArea() and getPerimeter() methods for returning the area and perimeter of the rectangle.

Note

To avoid naming conflict with improved versions of the GeometricObject , Circle , and Rectangle classes introduced in the next chapter, put classes GeometricObject , Circle , and Rectangle into a package named chapter9 . The package name must be mapped to the file system directory structure, so, place the files GeometricObject.java , Circle.java , and Rectangle.java into c:\book\chapter9 .


The GeometricObject , Circle , and Rectangle classes are shown in Listings 9.1, 9.2, and 9.3.


[Page 303]
Listing 9.1. GeometricObject.java
(This item is displayed on pages 303 - 304 in the print version)
 1    package   chapter9;  2 3   public class   GeometricObject { 4   private   String color =   "white"   ; 5   private boolean   filled; 6   private   java.util.Date dateCreated; 7 8  /** Construct a default geometric object */  9   public   GeometricObject() { 10  dateCreated =   new   java.util.Date();  11 } 12 13  /** Return color */  14   public   String getColor() { 15   return   color; 16 } 17 

[Page 304]
 18  /** Set a new color */  19   public void   setColor(String color) { 20   this   .color = color; 21 } 22 23  /** Return filled. Since filled is boolean,  24  so, the get method name is isFilled */  25   public boolean   isFilled() { 26   return   filled; 27 } 28 29  /** Set a new filled */  30   public void   setFilled(   boolean   filled) { 31   this   .filled = filled; 32 } 33 34  /** Get dateCreated */  35   public   java.util.Date getDateCreated() { 36   return   dateCreated; 37 } 38 39  /** Return a string representation of this object */  40   public   String toString() { 41   return   "created on "   + dateCreated +   "\ncolor: "   + color + 42   " and filled: "   + filled; 43 } 44 } 

Listing 9.2. Circle.java
(This item is displayed on pages 304 - 305 in the print version)
 1    package   chapter9;  2 3   public class   Circle    extends   GeometricObject  { 4   private double   radius; 5 6   public   Circle() { 7 } 8 9   public   Circle(   double   radius) { 10   this   .radius = radius; 11 } 12 13  /** Return radius */  14   public double   getRadius() { 15   return   radius; 16 } 17 18  /** Set a new radius */  19   public void   setRadius(   double   radius) { 20   this   .radius = radius; 21 } 22 23  /** Return area */  24   public double   getArea() { 25   return   radius * radius * Math.PI; 26 } 27 28  /** Return diameter */  29   public double   getDiameter() { 30   return   2   * radius; 31 } 32 

[Page 305]
 33  /** Return perimeter */  34   public double   getPerimeter() { 35   return   2   * radius * Math.PI; 36 } 37 38  /* Print the circle info */  39   public void   printCircle() { 40 System.out.println(   "The circle is created "   +  getDateCreated()  + 41   " and the radius is "   + radius); 42 } 43 } 

Listing 9.3. Rectangle.java
 1    package   chapter9;  2 3   public class   Rectangle    extends   GeometricObject {  4   private double   width; 5   private double   height; 6 7   public   Rectangle() { 8 } 9 10   public   Rectangle(   double   width,   double   height) { 11   this.   width = width; 12   this.   height = height; 13 } 14 15  /** Return width */  16   public double   getWidth() { 17   return   width; 18 } 19 20  /** Set a new width */  21   public void   setWidth(   double   width) { 22   this.   width = width; 23 } 24 25  /** Return height */  26   public double   getHeight() { 27   return   height; 28 } 29 30  /** Set a new height */  31   public void   setHeight(   double   height) { 32   this.   height = height; 33 } 34 35  /** Return area */  36   public double   getArea() { 37   return   width * height; 38 } 39 40  /** Return perimeter */  41   public double   getPerimeter() { 42   return   2   * (width + height); 43 } 44 } 

The classes Circle and Rectangle extend the GeometricObject class. The reserved word extends tells the compiler that these classes extend the GeometricObject class, thus inheriting the methods getColor , setColor , isFilled , setFilled , and toString .


[Page 306]

The following code in Listing 9.4 creates objects of Circle and Rectangle , and invokes the methods on these objects.

Listing 9.4. TestCircleRectangle.java
 1    package   chapter9;  2 3   public class   TestCircleRectangle { 4   public static void   main(String[] args) { 5  Circle circle =   new   Circle(   1   );  6 System.out.println(   "A circle "   +  circle.toString()  ); 7 System.out.println(circle.getRadius()); 8 System.out.println(   "The radius is "   +  circle.getRadius()  ); 9 System.out.println(   "The area is "   +  circle.getArea()  ); 10 System.out.println(   "The diameter is "   +  circle.getDiameter()  ); 11 12  Rectangle rectangle =   new   Rectangle(   2   ,   4   );  13 System.out.println(   "\nA rectanlge "   +  rectangle.toString()  ); 14 System.out.println(   "The area is "   +  rectangle.getArea()  ); 15 System.out.println(   "The perimeter is "   + 16  rectangle.getPerimeter()  ); 17 } 18 } 

Since radius is 1 , color is white , and filled is false by default, the output is as shown in Figure 9.2. Note that TestCircleRectangle.java is stored in c:\book\chapter9 and the TestCircleRectangle class is in package chapter9 . Thus you have to run it from c:\book using the command java chapter9.TestCircleRectangle with the complete class name.

Figure 9.2. The methods in GeometricObject are inherited in Circle and Rectangle .


Note

Contrary to the conventional interpretation, a subclass is not a subset of its superclass. In fact, a subclass usually contains more information and functions than its superclass.


Caution

Inheritance is used to model the is-a relationship. Do not blindly extend a class just for the sake of reusing methods. For example, it makes no sense for a Tree class to extend a Person class. A subclass and its superclass must have the is-a relationship.


 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net