Java Reference
In-Depth Information
The output from this program is shown here:
Here,
TwoDShape
defines the attributes of a “generic” two-dimensional shape, such
as a square, rectangle, triangle, and so on. The
Triangle
class creates a specific type of
TwoDShape
, in this case, a triangle. The
Triangle
class includes all of
TwoDObject
and
adds the field
style
, the method
area( )
, and the method
showStyle( )
. The triangle's style
is stored in
style
. This can be any string that describes the triangle, such as "filled", "out-
lined", "transparent", or even something like "warning symbol", "isosceles", or "rounded".
The
area( )
method computes and returns the area of the triangle, and
showStyle( )
dis-
plays the triangle style.
Because
Triangle
includes all of the members of its superclass,
TwoDShape
, it can ac-
cess
width
and
height
inside
area( )
. Also, inside
main( )
, objects
t1
and
t2
can refer to
width
and
height
directly, as if they were part of
Triangle
.
Figure 7-1
depicts conceptually
how
TwoDShape
is incorporated into
Triangle
.