Java Reference
In-Depth Information
and syntactic sugar (syntax that simplifies a language, making it “sweeter” to use). In
contrast, parametric and subtype are regarded as valid kinds of polymorphism.
This section introduces you to subtype polymorphism through upcasting and late
binding. We then move on to abstract classes and abstract methods, downcasting and
runtime type identification, and covariant return types.
Upcasting and Late Binding
Listing 2-27 ' s Point class represents a point as an x-y pair. Because a circle (in this
extend Point witha Circle classthatintroducesa radius field.Checkout Listing
2-32 .
Listing 2-32. A Circle class extending the Point class
class Circle extends Point
private int radius;
Circle(int x, int y, int radius)
super(x, y);
this.radius = radius;
int getRadius()
return radius;
Listing 2-32 ' s Circle class describes a Circle as a Point with a radius ,
whichimpliesthatyoucantreata Circle instanceasifitwasa Point instance.Ac-
complishthistaskbyassigningthe Circle instancetoa Point variable,asdemon-
strated here:
Circle c = new Circle(10, 20, 30);
Point p = c;
The cast operator is not needed to convert from Circle to Point because access
toa Circle instancevia Point 'sinterfaceislegal.Afterall,a Circle isatleasta
Point . This assignment is known as upcasting because you are implicitly casting up
Search WWH ::

Custom Search