• 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

Question on Overriding

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
We know that the private methods are not overridden.
Then how does the following code compile
class R {
private void printS1(){System.out.print("R.printS1 ");}//1
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 ");}//2
protected void printS2(){System.out.print("S.printS2 ");}
public static void main(String[] args) {
new S().printS1S2();
}}

We say that the method printS1 is not overriden. Then what is happening in the above code?
-Sanjana
 
Ranch Hand
Posts: 330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
S1 is a different method which happens to be the same method name in its superclass.
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Private access modifier makes a method implicitly final, which means it cannot be overridden. A private method of a superclass can be redeclared and redefined in its subclasses.
[ December 17, 2003: Message edited by: Vad Fogel ]
 
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 of overriding as making a method more specific for the subclass.
Think of the subclass taking the method from it's superclass and improving it. But if that method is private in the superclass it does not exist for the subclass, it's totally invlsible.
 
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In javaworld article "Private and final?"
 
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a good article.... Thanks surasak...
Private method and final works differently and treated differently by the JVM.... And one more interesting thing is that combination of both keywords "private" and "final" is treated differently again....
I can say that the article is good to read to know details about the "private" and "final" keywords...
 
sanjana narayanan
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank u everyone for replying.
I was expecting the same answer but in one of the certification guide by Simon Roberts/Barry Boone(not sure which author it was), they had specified that private methods can be overriden by private/default/protected/public methods. That's why i wanted to reconfirm things.
-Sanjana
 
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 sanjana narayanan:
Thank u everyone for replying.
I was expecting the same answer but in one of the certification guide by Simon Roberts/Barry Boone(not sure which author it was), they had specified that private methods can be overriden by private/default/protected/public methods. That's why i wanted to reconfirm things.
-Sanjana


I used that book, when i took SCJP1.2... I'm not sure whether they said so... Did u read that part carefully? I mean if they r talking about it another context... That kind of mistake is not for authors like them...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic