• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confused in function overriding

 
goel Ashish
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does the following code prints "animal eating" :-


And changing the modifier of eat method in Animal class from private to public results in printing :- "horse eating"
 
Vijay Tidake
Ranch Hand
Posts: 148
Hibernate Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In first case the method has a signature private void eat(), so if you are extending the Horse with the Animal the method is not get inherited in the Horse class.

So at runtime JVM unable to see polymorphic behavior.

But in second case as the signature of both the method match(overriding) so polymorphism comes in picture and hence Horse eat() will be called.

Hope I am able to put in correct words

Thanks
 
Rob Spoor
Sheriff
Pie
Posts: 20671
65
Chrome Eclipse IDE Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In short: private methods are never inherited and can never be overridden. They are instead shadowed if a sub class has a method with the same name and arguments.
 
goel Ashish
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
premkumar bhaskal
Greenhorn
Posts: 2
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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.
 
goel Ashish
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Am I right now??
 
Campbell Ritchie
Sheriff
Pie
Posts: 50289
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
goel Ashish
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Campbell Ritchie
Sheriff
Pie
Posts: 50289
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
goel Ashish
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@campbell Maybe I understood the same thing which you explained. Now your post and concept both are clear. Thnks.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50289
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome We got there in the end.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic