• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

doubt private method

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all

my code:

class Base{
private void amethod(int iBase){
System.out.println("Base.amethod");
}
}

class Over extends Base{
public static void main(String argv[]){
Over o = new Over();
int iBase=0;
o.amethod(iBase);
}

public void amethod(int iOver){
System.out.println("Over.amethod");
}
}

This code compile & run fine and output is "Over.amethod" .

my doubt is private method how do overitten any one explain above code .

Thanks & regards

venkat
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
im not sure if i understand your question correctly,
but we are allowed to override a superclass method given that we did not make the access more restrictive.

in your case,

class Base{
private void amethod(int iBase){
System.out.println("Base.amethod");
}
}


it is private, so methods that override amethod can have any access level, since private is the most restricitve. Therefore, in your class Over, a public access would definitely work, but not vice versa.

Hope this helps.
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
english learn language first.

try to post in correct English for people to understand your questions (and learning proper English will help you understand the answers better too).

Many of us are not native English speakers which makes it even more important to not write in pidgin.

I notice poor English is mainly exhibited here by people with Indian sounding names...
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ryan Wong:
im not sure if i understand your question correctly,
but we are allowed to override a superclass method given that we did not make the access more restrictive.



That's true, but that's not what is happening here. Private methods are not inherited and, therefore, they can not be overridden. Rather, as far as the subclass is concerned, private methods in the parent class don't even exist - they can't be accessed. That's why the subclass can define a method with the same signature. There's no naming conflict because the subclass doesn't realize that the superclass defined a method with that signature.

Take a look at this.
reply
    Bookmark Topic Watch Topic
  • New Topic