• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Basic question about Overload/Override

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Why this code outputs "Animal"? Can you explain? Is it a case of overloading instead override?
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 46
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 59
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic