• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Private Method overriding

 
Nidhi Juneja
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic