Java Reference
In-Depth Information
consistent interface. Thus, by combining inheritance with overridden methods, a superclass
can define the general form of the methods that will be used by all of its subclasses.
Applying Method Overriding to TwoDShape
To better understand the power of method overriding, we will apply it to the TwoDShape
class. In the preceding examples, each class derived from TwoDShape defines a method
called area( ) . This suggests that it might be better to make area( ) part of the TwoDShape
class, allowing each subclass to override it, defining how the area is calculated for the type
of shape that the class encapsulates. The following program does this. For convenience, it
also adds a name field to TwoDShape . (This makes it easier to write demonstration pro-
grams.)
Search WWH ::




Custom Search