aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt private method  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt private method  " Watch "doubt private method  " New topic
Author

doubt private method

venkataraman muthuvel
Greenhorn

Joined: Jan 28, 2004
Posts: 22
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

Joined: Jun 15, 2004
Posts: 66
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

Joined: Oct 12, 2000
Posts: 5093
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...


42
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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.


SCJP Tipline, etc.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt private method