Java Reference
In-Depth Information
features from its superclass. Furthermore, the subclass can add new features of its own.
Therefore, rather than create completely new classes from scratch, you can take advantage of
inheritance and reduce software complexity.
Inheritance can be viewed as a treelike, or hierarchical, structure wherein a superclass is
shown with its subclasses. Consider the diagram in Figure 10-1, which shows the
relationship between various shapes.
Shape
Circle
Rectangle
Square
FIGURE 10-1
Inheritance hierarchy
In this diagram,
Shape
is the superclass. The
class
es
Circle
and
Rectangle
are
derived from
Shape
, and the
class
Square
is derived from
Rectangle
. Every
Circle
and every
Rectangle
is a
Shape
. Every
Square
is a
Rectangle
.
The general syntax to derive a class from an existing class is:
1
0
modifier(s)
class
ClassName
extends
ExistingClassName modifier(s)
{
memberList
}
In Java,
extends
is a reserved word.
EXAMPLE 10-1
Suppose that we have defined a
class
called
Shape
. The following statements specify
that the
class
Circle
is derived from
Shape
:
public class
Circle
extends
Shape
{
.
.
.
}
Search WWH ::
Custom Search