Java Reference
In-Depth Information
11.1 Introduction
Object-oriented programming allows you to define new classes from existing classes.
This is called inheritance.
Key
Point
As discussed earlier in the topic, the procedural paradigm focuses on designing methods and
the object-oriented paradigm couples data and methods together into objects. Software design
using the object-oriented paradigm focuses on objects and operations on objects. The object-
oriented approach combines the power of the procedural paradigm with an added dimension
that integrates data with operations into objects.
Inheritance is an important and powerful feature for reusing software. Suppose you need
to define classes to model circles, rectangles, and triangles. These classes have many common
features. What is the best way to design these classes so as to avoid redundancy and make the
system easy to comprehend and easy to maintain? The answer is to use inheritance.
inheritance
why inheritance?
11.2 Superclasses and Subclasses
Inheritance enables you to define a general class (i.e., a superclass) and later extend it
to more specialized classes (i.e., subclasses).
Key
Point
You use a class to model objects of the same type. Different classes may have some com-
mon properties and behaviors, which can be generalized in a class that can be shared by other
classes. You can define a specialized class that extends the generalized class. The specialized
classes inherit the properties and methods from the general class.
Consider geometric objects. Suppose you want to design the classes to model geometric
objects such as circles and rectangles. Geometric objects have many common properties and
behaviors. They can be drawn in a certain color and be filled or unfilled. Thus a general class
GeometricObject can be used to model all geometric objects. This class contains the proper-
ties color and filled and their appropriate getter and setter methods. Assume that this class
also contains the dateCreated property and the getDateCreated() and toString()
methods. The toString() method returns a string representation of 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 Geomet-
ricObject class. Likewise, Rectangle can also be defined as a subclass of GeometricOb-
ject . FigureĀ 11.1 shows the relationship among these classes. A triangular arrow pointing to
the superclass is used to denote the inheritance relationship between the two classes involved.
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 parent class or a base class , and a
subclass as 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.
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 getter and setter methods.
The Circle class 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 Geomet-
ricObject class. In addition, it has the data fields width and height and their associated
getter and setter methods. It also contains the getArea() and getPerimeter() methods for
returning the area and perimeter of the rectangle.
The GeometricObject , Circle , and Rectangle classes are shown in Listings 11.1,
11.2, and 11.3.
VideoNote
Geometric class hierarchy
subclass
superclass
Note
To avoid a naming conflict with the improved GeometricObject , Circle ,
and Rectangle classes introduced in ChapterĀ  13, we'll name these classes
avoid naming conflicts
 
 
 
 
Search WWH ::




Custom Search