I recently saw this example code and I didn't know how I'd be able to easily find the answer to this question I had. In the code the Dog object, Cow object and Snake object are all declared as Animal objects. So is it valid to declare an object using a more generic class? For instance, could I declare any object as an Object (since all classes are subclasses of the object class)? What are the advantages/disadvantages of declaring specific or more general? Is it all for ease of readability?
class Animal {
void whoAmI() {
System.out.println("I am a generic Animal.");
}
}
class Dog extends Animal {
void whoAmI() {
System.out.println("I am a Dog.");
}
}
class Cow extends Animal {
void whoAmI() {
System.out.println("I am a Cow.");
}
}
class Snake extends Animal {
void whoAmI() {
System.out.println("I am a Snake.");
}
}
class RuntimePolymorphismDemo {
public static void main(String[] args) {
Animal ref1 = new Animal();
Animal ref2 = new Dog();
Animal ref3 = new Cow();
Animal ref4 = new Snake();
ref1.whoAmI();
ref2.whoAmI();
ref3.whoAmI();
ref4.whoAmI();
}
}
The output is
I am a generic Animal.
I am a Dog.
I am a Cow.
I am a Snake.