This happens because of the overriding...
In the supper class Animal -> void makeNoise()
In the sub class Dog-> void makeNoise()//overriden method & playDead()// this is unique for this class
Then you have assining a dog instance to the Animal reference ( in the loop)
In that case compiler check
Animal animal=new Dog();
first reference type (Animal) where there is playDead() method and runtime check the instance type. ok..
In your case there is no any method playDead() in Animal class..so compiler gives an error..
Hennry Smith wrote:
When we try to compile this code, the compiler says something like this:
cannot find symbol
The compiler is saying, "Hey, class Animal doesn't have a playDead() method".
Let's modify the if code block:
if(animal instanceof Dog) {
Dog d = (Dog) animal; // casting the ref. var.
d.playDead();
}
Please explain i am not able to understand this concept...???