Originally posted by Namit Puri:
I still can't understand that why the error is shown beacuase of the Exception declared on the Animal eat() method; even though at run time the eat() method used would be of the Dog version for refernce variable a.
In that exam watch, we refer Dog object as Animal type. Imagine when we have method with Animal type argument, someone can pass in Dog or Animal object type. Compiler take precaution in this case.
To summarise: At compile time, compiler only knows/cares that we invoke eat() method of Animal type. Compiler doesn't know what the real object is. It's JVM job.