Java Reference
In-Depth Information
Listing 2-24.
A fictional demonstration of multiple implementation inheritance
class Horse
{
void describe()
{
// Code that outputs a description of a horse's ap-
pearance and behaviors.
}
}
class Bird
{
void describe()
{
// Code that outputs a description of a bird's ap-
pearance and behaviors.
}
}
class FlyingHorse extends Horse, Bird
{
public static void main(String[] args)
{
FlyingHorse pegasus = new FlyingHorse();
pegasus.describe();
}
}
This class structure reveals an ambiguity resulting from each of
Horse
and
Bird
declaring a
describe()
method. Which of these methods does
FlyingHorse
in-
herit? A related ambiguity arises from same-named fields, possibly of different types.
Which field is inherited?
The Ultimate Superclass
AclassthatdoesnotexplicitlyextendanotherclassimplicitlyextendsJava's
Object
class(locatedinthe
java.lang
package—Iwilldiscusspackagesinthenextchapter).
and
Truck
classes extend
Vehicle
, which extends
Object
.