Given the following classes:
abstract public class Animal{
abstract public void greeting();
}
public class Dog extends Animal {
public void greeting() {
System.out.println("Woof!");
}
public void greeting(Dog another) {
System.out.println("Wooooooooof!")
}
}
public class BigDog extends Dog {
public void greeting() {
System.out.println("Woow!")
}
public void greeting(Dog another) {
System.out.println("Woooooooooooow!")
}
}
What is the difference between
Dog animal = new Dog()
and Animal animal = new Dog()
?
Because animal.greeting()
produces the exact same result.
I know theoretically that later creates an instance of Dog but storing that in the superclass (or abstract in this class). But what does that really do? They both will check Animal class since Dog is extended to Animal.. Need some enlightenment here.
What is the difference between Animal animal = new BigDog()
and Dog animal = new BigDog()
?
Won't they eventually check Animal class? Since BigDog is extended to Dog and Dog is extended to Animal.
Please provide concrete examples to explain this. I understand this in theory but having a hard time understanding what is actually happening behind the scene. Thanks for taking your time to read this. I only know Java so I'd appreciate if you explain using Java..
**EDIT: I have seen the duplicate but still only touching the surface of polymorphism and its relationship with abstract class. Please go read it. My question is specifically asks for the link between abstract and regular classes and between each regular class. I don't see how this is duplicate because I am asking for what is actually happening behind the scene: BigDog extends Dog and Dog extends Animal. So can someone explain what is actually happening here instead of flagging posts like a machine?