I am still confused.
Isn't it like :- During compilation binding of method call is done in accordance with the type of reference variable but at the runtime jvm will execute that function of which object is created. Using this concept in the above example :- private method eat is not inherited and therefore callEat method's call to eat function should execute the eat method of horse class.
I just want to add few points to vijay's post.
In the first case , the call a.callEat(); will call the Animal version of method callEat().
Now this is obvious since you are not overriding callEat() method.
Now the case of method call 'eat()' , as vijay rightly pointed out.
So at runtime JVM unable to see polymorphic behavior.
the JVM will call Animal's version of eat() method.
One more point had you overridden the callEat() method also (as done in following code) you will get an output "Horse eating" , even though you are not overriding eat() method.
Hope this clarifies your doubt.
One point to ponder is that inherting method does not mean that the method gets copy-pasted to subtype class.
As far as i have understood this thing the flow in like that:-
At compile time the function call is binded to the function of that class of which type reference variables is.
Now at run time the jvm first sees if the function can be overridden or not. If it can be then jvm checks if is being overridden or not, If yes then it executes the overridden method. Else it executes the compile time binded method.
Campbell Ritchie wrote:More likely the compiler binds the declared type to the object for static members, or fields, or private instance methods, and permits the JVM to bind at runtime for non-private instance methods.
Didn't understood what you explained . Can you elaborate?
Not certain, but it is likely the compiler does not bind anything to public instance methods. The JVM does that. The compiler does however bind the type for private methods, or fields, or anything static.
This is not actually something you need to know. What you do need to know is that accessible instance methods are inherited and can be overridden (eg public access, or default access inside the same package). Private methods, fields, and anything static cannot be overridden.