File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt private method

 
venkataraman muthuvel
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Dan T
Ranch Hand
Posts: 66
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • 0
  • Mark post as helpful
  • send pies
  • 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...
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic