Sub topic 4
Use Subtypes and Understand Method Dispatch
A subtype is a class that extends another class. When Dog extends Animal, Dog is a subtype of Animal. Every dog is also an animal, so Java lets you treat a dog as an animal. This chapter shows how that works and how Java picks which method to run.
What Is a Subtype?
A subtype can be stored in a variable of its parent's type:
Animal pet = new Dog("Rex");The variable pet has the type Animal. The object it holds is a Dog. Java keeps track of both facts.
> The variable's type is what Java checks when it compiles your code. The object's real type is what runs when the program runs.
What Is Polymorphism?
Polymorphism is a word that means "many forms." One call can behave differently depending on which object receives it. A Dog and a Cat can both answer the same call, speak(), in their own way.
What Is Method Dispatch?
Method dispatch is the step where Java chooses which version of a method to run. When the choice happens while the program runs, it is called dynamic dispatch. Java looks at the real object, not the variable type.
flowchart TD
A["Call pet.speak()"] --> B{"What is pet really?"}
B -->|Dog| C["Dog version runs"]
B -->|Cat| D["Cat version runs"]
B -->|Animal| E["Animal version runs"]caption: How Java chooses a method when pet.speak() is called alt: A flowchart. The call pet.speak() leads to a check of the real object type. A Dog runs the Dog version, a Cat runs the Cat version, and a plain Animal runs the Animal version.
A Worked Example
Save this code as Main.java:
class Animal {
private final String name;
Animal(String name) {
this.name = name;
}
String getName() {
return name;
}
String speak() {
return getName() + " makes a sound";
}
}
class Dog extends Animal {
Dog(String name) {
super(name);
}
@Override
String speak() {
return getName() + " says Woof";
}
}
class Cat extends Animal {
Cat(String name) {
super(name);
}
@Override
String speak() {
return getName() + " says Meow";
}
}
public class Main {
public static void main(String[] args) {
Animal[] pets = { new Dog("Rex"), new Cat("Tom"), new Animal("Mystery") };
for (Animal pet : pets) {
System.out.println(pet.speak());
}
}
}The output is:
Rex says Woof
Tom says Meow
Mystery makes a soundThe loop calls speak() on each item. The variable type is always Animal, yet each line prints the right message. Java chose the method from each object's real class.
| Term | Plain meaning |
|---|---|
| Subtype | A class that extends another class |
| Polymorphism | One call that acts differently for different objects |
| Dynamic dispatch | Java choosing the method at run time |
> Warning: Java checks the variable type first. If Animal has no fetch() method, then pet.fetch() will not compile, even when the object is a Dog that has one.
Exercises
Exercise
Suppose you add class Cow extends Animal, with speak() returning getName() + " says Moo", and the array holds new Cow("Bess"). What line prints for that item?
Show answer
Bess says Moo. The loop calls speak() on the Cow object, so Java runs the Cow version.
Exercise
Why can an Animal[] array hold a Dog?
Show answer
Because Dog extends Animal, every Dog is also an Animal, so a Dog is a valid value for an Animal variable.