• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

OverRiding a private method

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry previous post a little difficult to read, my first ever post. This is the new and improved Post.
The following are 2 sample programs that I altered from a study book. I don't understand why the output changes when I change Animal's eat method's Access level. I thought both would have outputted Horse Eating Horse Food. If anyone can explain the reason for this, please let me know

Output:Horse Eating Horse Food
---------------------------------------------------------

Output: Generic Animal Eating Generically
 
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this case, I have to say that private methods possess more precedence over public methods...
Since the class Animal found its own eat method possess more private method, it's calling its own eat method, instead of calling more public method in the subclass.....
Correct me, if I am wrong... It's my personal opinion on it... :roll:
 
Paul Callaly
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That makes good sense. It's enough to help me move on to the next topic. That problem's been bugging me all afternoon
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think for private methods, binding occurs at compile time. So is the behaviour.
Correct me if i am wrong.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When the getEat() method is invoked, it simply invokes the eat() method. This invocation occurs in the Animal class, and thus, the eat() method must be resolved from there.
In the first case, both classes Animal and Horse declare one public method called eat(). Since these methods are public, the one in class Horse is said to override the one in class Animal. Thus, the invocation of method eat() in class Animal boils down to invoking the overriding method in class Horse. Hence, the output.
In the second case, the method eat() in class Animal is declared private, and thus, it cannot be overridden (JLS 8.4.6.1 Overriding (by Instance Methods)). This doesn't mean that class Horse is not allowed to declare a method called eat(), just that the method eat() in class Horse does not override the method eat() in class Animal. When the method eat() is invoked in class Animal, the call will resolve to the private method in the same class since there is no overriding. See JLS 15.12 Method Invocation Expressions for an in-depth explanation of the method resolution process. I have written an article ( JLS 15.12 in Plain English) that tried to make it easier to understand how the whole method resolution process works.
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Valentin Crettaz:

In the second case, the method eat() in class Animal is declared private, and thus, it cannot be overridden (JLS 8.4.6.1 Overriding (by Instance Methods)). This doesn't mean that class Horse is not allowed to declare a method called eat(), just that the method eat() in class Horse does not override the method eat() in class Animal. When the method eat() is invoked in class Animal, the call will resolve to the private method in the same class since there is no overriding. See JLS 15.12 Method Invocation Expressions for an in-depth explanation of the method resolution process. I have written an article ( JLS 15.12 in Plain English) that tried to make it easier to understand how the whole method resolution process works.


Hi Valentin,
So do u mean that the method in Horse is its own method, which is not related to the eat method in Animal, don't u? There is no relationship between those two methods, isn't it? Just wanna get more clarification...
Thank you for your explanation....
 
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the point is that the private method is not even visible or available to the sub class. As far as the subclass is concerned it does not exist.
 
reply
    Bookmark Topic Watch Topic
  • New Topic