Java Reference
In-Depth Information
The following Play interface represents objects that can play fetch and play
catch, which is implemented by our Dog class.
public interface Play
{
public void playFetch();
public void playCatch();
}
The following Dog class extends Mammal and implements the Play interface.
public class Dog extends Mammal implements Play
{
public void playFetch()
{
System.out.println(“Dog is fetching”);
}
public void playCatch()
{
System.out.println(“Dog is catching”);
}
public void sleep()
{
System.out.println(“Dog is sleeping”);
}
}
I want to point out the various forms that a Dog object can take on. Through
polymorphism, a Dog can take on the following forms:
Dog. Certainly a Dog object can take the form of a Dog.
Mammal. A Dog object is a Mammal object because Dog extends the
Mammal class.
Play. A Dog object is a Play object because Dog implements Play.
Object. A Dog object is an Object because Dog extends Mammal and
Mammal extends Object.
Therefore, the following four statements are all valid:
Dog fido = new Dog();
Mammal rover = new Dog();
Play spot = new Dog();
Object pooch = new Dog();
Search WWH ::




Custom Search