File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Confused in function overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Confused in function overriding" Watch "Confused in function overriding" New topic
Author

Confused in function overriding

goel Ashish
Greenhorn

Joined: May 14, 2011
Posts: 21
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

Joined: Nov 04, 2008
Posts: 146

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


The important thing is not to stop questioning.Curiosity has its own reason for existing.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
goel Ashish
Greenhorn

Joined: May 14, 2011
Posts: 21
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

Joined: Mar 16, 2011
Posts: 2

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

Joined: May 14, 2011
Posts: 21
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

Joined: Oct 13, 2005
Posts: 38007
    
  22
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

Joined: May 14, 2011
Posts: 21
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

Joined: Oct 13, 2005
Posts: 38007
    
  22
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

Joined: May 14, 2011
Posts: 21
@campbell Maybe I understood the same thing which you explained. Now your post and concept both are clear. Thnks.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
You're welcome We got there in the end.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confused in function overriding
 
Similar Threads
Unable to understand reason for ClassCastException
Dynamic method invocation
Polymorphism
my problem related to overriding
Overloaded method question