• 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

hiding private method in subclass

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Water! People swim in water! Even tiny ads swim in water:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic