jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes OverRiding a private method 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 "OverRiding a private method" Watch "OverRiding a private method" New topic
Author

OverRiding a private method

Paul Callaly
Greenhorn

Joined: Dec 16, 2003
Posts: 12
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
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
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:


Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
Paul Callaly
Greenhorn

Joined: Dec 16, 2003
Posts: 12
That makes good sense. It's enough to help me move on to the next topic. That problem's been bugging me all afternoon
Anand Ko
Ranch Hand

Joined: Dec 03, 2003
Posts: 79
I think for private methods, binding occurs at compile time. So is the behaviour.
Correct me if i am wrong.


Anand<br />SCJP 1.4, SCWCD 1.4, SCEA 5.0(1/3)
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
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....
Steven Broadbent
Ranch Hand

Joined: Dec 10, 2002
Posts: 400
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.


"....bigmouth strikes again, and I've got no right to take my place with the human race...."<p>SCJP 1.4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: OverRiding a private method
 
Similar Threads
Unable to understand reason for ClassCastException
SCJP 6 study guide page 107 (Overridden Methods)
Polymorphism
my problem related to overriding
overriding