Java Reference
In-Depth Information
area( ) is given a placeholder implementation that simply informs the user that this method
must be overridden by a subclass. Each override of area( ) supplies an implementation that
is suitable for the type of object encapsulated by the subclass. Thus, if you were to imple-
ment an ellipse class, for example, then area( ) would need to compute the area( ) of an
ellipse.
There is one other important feature in the preceding program. Notice in main( ) that
shapes is declared as an array of TwoDShape objects. However, the elements of this array
are assigned Triangle , Rectangle , and TwoDShape references. This is valid because, as
explained, a superclass reference can refer to a subclass object. The program then cycles
through the array, displaying information about each object. Although quite simple, this il-
lustrates the power of both inheritance and method overriding. The type of object referred
to by a superclass reference variable is determined at run time and acted on accordingly. If
an object is derived from TwoDShape , then its area can be obtained by calling area( ) . The
interface to this operation is the same no matter what type of shape is being used.
Using Abstract Classes
Sometimes you will want to create a superclass that defines only a generalized form that
will be shared by all of its subclasses, leaving it to each subclass to fill in the details. Such
a class determines the nature of the methods that the subclasses must implement but does
not, itself, provide an implementation of one or more of these methods. One way this situ-
ation can occur is when a superclass is unable to create a meaningful implementation for a
method. This is the case with the version of TwoDShape used in the preceding example.
The definition of area( ) is simply a placeholder. It will not compute and display the area
of any type of object.
As you will see as you create your own class libraries, it is not uncommon for a method
to have no meaningful definition in the context of its superclass. You can handle this situ-
ation in two ways. One way, as shown in the previous example, is to simply have it report
a warning message. While this approach can be useful in certain situations—such as de-
bugging—it is not usually appropriate. You may have methods which must be overridden
by the subclass in order for the subclass to have any meaning. Consider the class Triangle .
It is incomplete if area( ) is not defined. In this case, you want some way to ensure that a
subclass does, indeed, override all necessary methods. Java's solution to this problem is the
abstract method .
An abstract method is created by specifying the abstract type modifier. An abstract
method contains no body and is, therefore, not implemented by the superclass. Thus, a sub-
class must override it—it cannot simply use the version defined in the superclass. To de-
clare an abstract method, use this general form:
abstract type name ( parameter-list );
Search WWH ::




Custom Search