Alfonso Saenz wrote:According what I have read is the object type who determines the method. Then.. Since object is Lizard, why invokes Reptile method?
Alfonso Saenz wrote:Thanks in advance and sorry for my ignorance.
Alfonso Saenz wrote:According what I have read is the object type who determines the method. Then.. Since object is Lizard, why invokes Reptile method?
Alfonso Saenz wrote:I knew the method was not overridden
Alfonso Saenz wrote:but I thought that as a hidden method, was the lizard's which is invoked.
Alfonso Saenz wrote:In this case both objects are Lion() and both will print the output given by Lion's method.
There is another issue with Exceptions. NotHungryException is not declared or treated in main()
Alfonso Saenz wrote:Thanks, but... what can I do with a cow??
Vani Dasu wrote:'a' being a reference of Animal goes to eat() present in the Animal class, then checks on the access specifiers - which is public hence it knows it can be inherited. Now as 'a' points to the object of Lion class and eat() can be inherited, goes and checks in Lion class. JVM finds the eat() in Lion and hence executes "Lion is always hungry".
Hope i am right.
Vani Dasu wrote:If my understanding is right, what would happen if 'a' after realizing that eat() can be inherited, checks in Lion but doesn't find any eat() then it comes back to Animal and executes eat() of Animal?
Consider Paul's rocket mass heater. |