File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Basic question about Overload/Override Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Basic question about Overload/Override" Watch "Basic question about Overload/Override" New topic

Basic question about Overload/Override

Front Enista

Joined: Nov 04, 2010
Posts: 6

Why this code outputs "Animal"? Can you explain? Is it a case of overloading instead override?
Rahul Saple
Ranch Hand

Joined: Aug 02, 2006
Posts: 46
The method eat() in class Animal is private and hence is not visible outside of the class(including any subclasses). The eat method of the subclass (Horse) doesn't override the superclass' method as it isn't inherited by the class.
It just has the same name which is what makes it confusing.
Rahul Saple
Ranch Hand

Joined: Aug 02, 2006
Posts: 46
Also, the polymorphic behavior of the reference 'a' would come into picture only if the method was overridden. Changing the signature of the eat method in the superclass to anything but private would give you the desired result.
The way you have defined the method is neither overloading nor overriding.
John Pradeep.v
Ranch Hand

Joined: Jul 21, 2008
Posts: 59
you can remember the logic behind this behvior by understanding the bytecode generated.
for normal method call, the compiler generates an "invoke" instruction which resolves the target class type at runtime... however, when a private method is called the compiler generates an "invokespecial" instruction which doesnt try to resolve the target method based on the instance type.

It is sorta covered in the JavaRanch Style Guide.
subject: Basic question about Overload/Override
Similar Threads
Overloaded question
overridden and overloading question
Confused in function overriding
Overloading private method in Parent class in Child class
If i want to report a bug