aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Private Method overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Private Method overriding" Watch "Private Method overriding" New topic
Author

Private Method overriding

Nidhi Juneja
Greenhorn

Joined: Oct 07, 2002
Posts: 27
Hi folks!!preparing for SCJP...i am unclear about the case when private methods are overridden in the subclass then which method is called that of the base class or the derived class....read somewhere that private methods are statically bound i.e. no matter the derived class defines the method with same signature as private method in base class the method will call the private method in the class where it is defined..... My answers don't tally with this rule.
Serdar Ozturk
Greenhorn

Joined: Aug 12, 2002
Posts: 14
Provate methods are not visible to the subclass, so it cannot override them. If you define any private method of the superclass with the same signature then it becomes a completely new method (not overridden)
To prove this, you can try illegal implementation of overriding.
Eg;
class C{
int getIt(){ return 0;}
}
class D extends C{
void getIt(){} // COMPILER ERROR (different method signature)
}

but the following code compiles fine
class C{
private int getIt(){ return 0;}
}
class D extends C{
void getIt(){} //OK
}
Serdar
Originally posted by Nidhi:
Hi folks!!preparing for SCJP...i am unclear about the case when private methods are overridden in the subclass then which method is called that of the base class or the derived class....read somewhere that private methods are statically bound i.e. no matter the derived class defines the method with same signature as private method in base class the method will call the private method in the class where it is defined..... My answers don't tally with this rule.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Nidhi,
Welcome to Javaranch
We'd like you to read the Javaranch Naming Policy and change your publicly displayed name (change it here) to comply with our unique rule. Thank you.
PS: Quote from the naming policy:

For your publicly displayed name, use a first name, a space, and a last name. Obviously fictitious names or improperly formatted names may be locked out.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Three points:
1. private methods with same name/signature in super/sub classes have no relationships, completely independent.
2. private methods can be accessed by other methods of the same class only. Therefore, if a method in class A is accessing a private method Z, it will invoke Z in class A even though another method named Z is also present in super/sub classes of A.
3. However, an interesting situation arises when the method that is accessing the private method is itself an inherited method and there are two private methods in super and sub class with same name and signature. Now which method this inherited method will invoke.

The result will be in "In super class".
[ October 07, 2002: Message edited by: Barkat Mardhani ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Private Method overriding