Chapter 2, Question 8
This program will throw and exception at line 13. Dog does NOT have sniff() method.
If we comment out line 13, then the program will compile and run.
Observe line 12 ((Dog)new Hound()).bark(); At compile time, value of this instance variable is Dog, but at run-time, instance of this variable is Hound.
Below is a link that explains this concept. Even though it uses C programming language, one can grasp the explanation
The following is my understanding of this concept, which I also posted on coderanch.com:
You have a Facebook application where DOG does the following: eat(), sleep(), play(), walk(), run(), rollOver(), and (of course) bark()
So, application is a drop-down which holds type DOG, and the only entries in the drop-down are objects of type DOG, i.e. HOUND, POODLE, BEAGLE, LABORADOR.
You cannot have the drop-down perform, say HOUND.biteMailman(), because that method is not part of DOG (at least, not a well-trained DOG ;-)) . In other words, compiler ensures that all of DOG's children (i.e. HOUND, POODLE, BEAGLE, LABORADOR) can perform the consistent actions as DOG. However during run-time, the children perform DOG's action according to their own implementation (after all, each breed of DOG has a distinct way of doing things)
Please note that polymorphism applies to methods of a class, not variables, as demonstrated in wiki.answers. Hat-tip to Coderanch.com.