aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes hiding private method in subclass Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "hiding private method in subclass" Watch "hiding private method in subclass" New topic
Author

hiding private method in subclass

Rodge Thomas
Ranch Hand

Joined: Jul 25, 2002
Posts: 38
FROM:
www.danchisholm.net/august04/topic/inherit.html
class R {
private void printS1(){System.out.print("R.printS1 ");}
protected void printS2() {System.out.print("R.printS2 ");}
protected void printS1S2(){printS1();printS2();}
}
class S extends R {
private void printS1(){System.out.print("S.printS1 ");}
protected void printS2(){System.out.print("S.printS2 ");}
public static void main(String[] args) {
new S().printS1S2();
}
}
What is the result of attempting to compile and run the above program?
a. Prints: R.printS1 R.printS2
b. Prints: R.printS1 S.printS2
c. Prints: S.printS1 R.printS2
d. Prints: S.printS1 S.printS2
e. Runtime Exception
f. Compiler Error
g. None of the Above
ANSWER: B
EXPLANATION:
Prints: R.printS1 S.printS2 Method R.printS1 is private and therefore is not inherited by class S and is not overridden by method S.printS1. In contrast, method R.printS2 is protected and therefore is inherited by class S and is overridden by method S.printS2. R.printS1S2 therefore calls the R.printS1 and S.printS2 methods.
----
I was confident the answer was D. According to the explanation above:
"Method R.printS1 is private and therefore is not inherited by class S and is not overridden by method S.printS1."
I agree. But, I still think the printS1() of S should be called, because the printS1() of S 'hides' the printS1() of R.
Furthermore, private methods are bound at compile time anyway.
Any insight will be greatly appreciated.
[ August 20, 2002: Message edited by: Rodge Thomas ]

SCJP, SCWCD
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
The following quote from the
Java Language Specification, Section 8.4.6.3, Requirements in Overriding and Hiding.

Note that a private method cannot be hidden or overridden in the technical sense of those terms. This means that a subclass can declare a method with the same signature as a private method in one of its superclasses, and there is no requirement that the return type or throws clause of such a method bear any relationship to those of the private method in the superclass.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi There:
This is surprising. But, it seems that rule is:
Any calls to any private data members or private member methods withing the body of any inherited method will resolve to the base class' private data members and private member methods, even though the derived class has same private data members and member methods declared again.
Here is code that demonstrate this point:

[ August 20, 2002: Message edited by: Barkat Mardhani ]
[ August 20, 2002: Message edited by: Barkat Mardhani ]
Paul Villangca
Ranch Hand

Joined: Jun 04, 2002
Posts: 133
Hi Barkat,
Any calls to any private data members or private member methods withing the body of any inherited method will resolve to the base class' private data members and private member methods, even though the derived class has same private data members and member methods declared again.

Thet's quite a generalization. Here's my take on your example:
By declaring member methods private, these methods are not inherited by the subclass. In your example, the method printS1S2() is inherited by class S from R. But R's printS1() method isn't(it's private), so the printS1() method of S doesn't override the one in R, and consequently late (runtime) binding doesn't apply.
For member variables, I think the value accessed (base vs subclass) is dependent on the location of the calling method. In fact, the access modifier has no effect on the variable accessed, try changing 'private i' to 'i' or even 'public i' on either (or both) classes, the result will still be the same.
Hope this helps (and hoping I'm right )
Paul
P.S. Heh, this is what I get for not reading all the earlier posts. Anyway...
Hi Rodge,
The important thing to note is that it doesn't override R.printS1(), so (as I stated above) late-binding doesn't apply.
[ August 21, 2002: Message edited by: Paul Villangca ]
[ August 21, 2002: Message edited by: Paul Villangca ]
 
 
subject: hiding private method in subclass