| Author |
private vs. public
|
Suiram Namuen
Greenhorn
Joined: Jan 03, 2006
Posts: 3
|
|
Found this one recently: according to the solution the result was: Parent's method2() Parent's method1() and if the first methong was not private but public, protected or default, it would have been: Parent's method2() Child's method1() Can someone help me understanding this? Why is the private method of the Parent class invoked at all?
|
 |
Thomas De Vos
stable boy
Ranch Hand
Joined: Apr 12, 2003
Posts: 425
|
|
|
private methods cannot be overridden, they will behave as if they were declared as final.
|
Try your free <a href="http://www.javacertificate.com" target="_blank" rel="nofollow">SCJP 1.4</a> certification centre.<br />Try your free <a href="http://www.j2eecertificate.com" target="_blank" rel="nofollow">SCWCD</a> certification centre.<br />Try your free <a href="http://www.ejbcertificate.com" target="_blank" rel="nofollow">SCBCD</a> certification centre.<br />Try your <a href="http://www.webspherecertificate.com" target="_blank" rel="nofollow">Websphere (Test 285) </a> certification centre.<br />Try your <a href="http://www.j2mecertificate.com" target="_blank" rel="nofollow">SCMAD</a> certification centre. (New)<br /> <br /><a href="http://blogs.javacertificate.com" target="_blank" rel="nofollow">Java/J2EE Certification Blogging</a>
|
 |
Corey McGlone
Ranch Hand
Joined: Dec 20, 2001
Posts: 3271
|
|
Originally posted by Thomas De Vos: private methods cannot be overridden, they will behave as if they were declared as final.
Actually, I don't think that's quite accurate. It is true that a private method can not be overridden - the reason being that a private method is not inherited by the subclass. However, if you declare a method in the subclass with the same signature of a final method in the superclass, you'll get a compilation error. If you declare a method in the subclass with the same signature of a private method in the superclass, you'll get no such error. Here's a link to an article I wrote about the private modifier. In addition, here's an excerpt from that article:
A private member method is not inherited by subclasses. Therefore, the method can not be overridden. It can't even be accessed by subclasses. A final member method can not be overridden but it can be accessed from the subclass - it is still inherited. While it causes a compiler error to define a method in a subclass with the same signature of a final method in the superclass, defining a method in the subclass with the same signature as the parent's private method is allowed. Why? The subclass doesn't know about the private method, anyway - it's private to the parent class.
|
SCJP Tipline, etc.
|
 |
Suiram Namuen
Greenhorn
Joined: Jan 03, 2006
Posts: 3
|
|
|
Thanks for your info. I enjoyed to read the comprehensive article about "private".
|
 |
Bob Law
Ranch Hand
Joined: Dec 16, 2005
Posts: 50
|
|
|
I have to admit I am still confused. Why does Parent's method1() ever execute? Shouldn't it call method1 from the child?
|
 |
Thomas De Vos
stable boy
Ranch Hand
Joined: Apr 12, 2003
Posts: 425
|
|
Bob, Are you clear that method2() is called from the Parent class? If so, method1 will be called from within method2 from the Parent class. But which method1? Is it method1 from Parent or from the Child class? Because method1 from Parent is private and hence behaves as final for the Parent class, method1 is executed. For the Child class, method1 does not override method1 correctly from the Parent class and is a method like any other for the compiler and as such does not cause a compilation problem, as Corey explained this would have caused a compilation error when the method was defined as final.
|
 |
Corey McGlone
Ranch Hand
Joined: Dec 20, 2001
Posts: 3271
|
|
I have to admit I am still confused. Why does Parent's method1() ever execute? Shouldn't it call method1 from the child? Here's the way I think of it, Bob. Perhaps this will help you understand what is happening. First, we have this code: Pretty simple - we're creating a Child object and assigning it to a Parent reference variable. Next we invoke method2() on that object. Now, what does the JVM do? First, it looks at the Child class for a method called method2(). Does it find one? Of course not, there's no such method defined. So, next, the JVM looks at the parent class of Child, Parent, for a method called method2(). Sure enough, it finds a matching method and executes it. Within that method, we find this code: The println statement is very straightforward. However, the invocation of method1() is the troublesome part. At this point, we are essentially executing within our Parent object. So, the JVM looks for a method called method1() within the Parent class. Sure enough, it finds one. Now, if the method were not declared as private, the JVM would have to check the runtime type of our object and look for an overridden method. But that's not the case. The method is private, therefore it can not be overridden, and no such check is required. The JVM simply executes Parent.method1(). Hopefully, that helps.
|
 |
Bob Law
Ranch Hand
Joined: Dec 16, 2005
Posts: 50
|
|
|
Okay thank you very much Corey that makes perfect sense.
|
 |
 |
|
|
subject: private vs. public
|
|
|