Sub topic 2
One Variable, Many Kinds of Objects
In the last chapter, you saw that a Dog class can extend an Animal class. That means every Dog is also an Animal. This chapter shows what that lets you do with variables.
Two Types for One Thing
When you write a line like this, there are two types in play:
Animal pet = new Dog();- The declared type is the type written before the variable name. Here it is
Animal. It is fixed when you compile the code. - The runtime type is the class of the object that was actually built with
new. Here it isDog. It belongs to the object, not the variable.
A reference is a variable that points to an object instead of holding the object itself. So pet is a reference of type Animal that points to a Dog object.
Storing a subclass object in a superclass variable is called upcasting. Java does it for you automatically, because a Dog is always a valid Animal.
A Worked Example
class Animal {
String name() {
return "some animal";
}
}
class Dog extends Animal {
void fetch() {
System.out.println("Fetching the ball!");
}
}
class Cat extends Animal {
}
public class Main {
public static void main(String[] args) {
Animal pet = new Dog();
System.out.println(pet.name());
System.out.println(pet.getClass().getSimpleName());
// pet.fetch(); // Does not compile: Animal has no fetch() method
pet = new Cat();
System.out.println(pet.getClass().getSimpleName());
Animal[] zoo = { new Dog(), new Cat(), new Animal() };
for (Animal a : zoo) {
System.out.println(a.getClass().getSimpleName());
}
}
}Output:
some animal
Dog
Cat
Dog
Cat
AnimalHere is what happens:
petis declared asAnimal, but it points to aDog.getClass()asks the object for its runtime type. It answersDog.- The same variable then points to a
Cat. The declared type staysAnimal. Only the object changed. - The array holds three kinds of objects under one declared type.
The Declared Type Decides What You May Call
The compiler only looks at the declared type. It checks that the method you call exists in that type. Animal has no fetch() method, so pet.fetch() is rejected even though the object really is a Dog.
| Question | Answered by | When |
|---|---|---|
| Which methods can I call? | Declared type | Compile time |
| What kind of object is it? | Runtime type | Run time |
Why would you want this limit? It lets one piece of code work with many kinds of objects. A method that takes an Animal works for dogs, cats, and any subclass written later. In the coming chapters, you will see how each object can still act in its own way when you call a method through that shared type. Getting the Dog back out with a cast is covered later too.
Exercise
Given Animal a = new Cat();, what is the declared type of a, what is its runtime type, and would a.name() compile?
Show answer
The declared type is Animal and the runtime type is Cat. Yes, a.name() compiles, because name() is defined in Animal, the declared type.
Exercise
Why does Animal pet = new Dog(); pet.fetch(); fail to compile when the object is a Dog?
Show answer
The compiler checks method calls against the declared type, Animal. Animal has no fetch() method, so the call is rejected. The runtime type does not matter at compile time.