If your instance is Mammal
and if you are calling methodA()
(which is overriden in Mammal
), java will call Mammal
's methodA()
. This is called Dynamic Polymorphism
To call methodA()
of Animal
from Mammal
class, you need to invoke it using super
keyword, within the Mammal
class, something like below
class Mammal extends Animal{
void methodA(){
super.methodA();// --> calling Animal's methodA()
}
void methodAB(){
System.out.println("Two child class Dog and Cat I have");
}
}
Also, below statement will cause ClassCastException
as Animal
IS NOT A Mammal
(where as a Mammal IS A Animal )
Animal A1= new Animal();
Mammal M1 =(Mammal)A1;
M1.methodA();