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 a base
class reference can refer to a derived class object. The program then cycles through the array,
displaying information about each object. Although quite simple, this illustrates the power
of both inheritance and method overriding. The type of object stored in a base class reference
variable is determined at runtime 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.
Do'stlaringiz bilan baham: |