wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding private method 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 "Overriding private method" Watch "Overriding private method" New topic
Author

Overriding private method

Sadhi Mushrif
Greenhorn

Joined: May 09, 2003
Posts: 11
This is from Marcus discussion group :
1 public class Parent {
2 private void print(){
System.out.println(" From Parent");
}
public static void main(String args[]){
Parent p1 = new Parent();
p1.print();
p1 = new Chield();
p1.print();
}
}
class Chield extends Parent{
public void print(){
System.out.println(" From chield");
}
}
output will be
B. From Parent
From chield
C. From Parent
From Parent
The correct answer is C. But when I remove "private" in line 2, the out is B.
Would someone pls explain why "private Method print() in class Parent" are not overrided?
Does anyone know the answer...........
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Here is the way The Java Programming Language explains it (3.3.4):
A method can be overridden only if it is accessible. If the method is not accessible then it is not inherited, and if it is not inherited it can�t be overridden.
For example, a private method is not accessible outside its own class. If a subclass defines a method that coincidentally has the same signature and return type as the superclass�s private method, they are completely unrelated--the subclass method does not override the superclass�s private method.
s khosa
Ranch Hand

Joined: May 15, 2003
Posts: 72
Should the output in any case not be "from parent" followed by "from chield"? Statement p1= new Chield() sets p1 variable to a reference of type Chield() class. And at run time, method() is invoked as per the runtime reference in the variable? Why does it invoke Parent classes method still?
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Well now whats happening in this case is that the Parent's private print() method is private that means the Chield class will not inherit it. Even if the Chield class has a method by the name print() it is not inheritance. Now when p1 refers to Parent its fine but when it refers to Chield it can only call those methods on the Chield class which have been inherited. Now as the method print() hasnt been inherited by the Chield class so it can't call that specific method.
Let's see this by example

Notice that a line has been commented out had that not been done the program wouldn't had compiled reason - the superclass doesnt has a method the supcl.submethod() is refering to. But notice the comment second invocation. In this though the var subcl refers to subclass can invoke a method of the superclass. Had the subclass had a method called supermethod() in it then the compiler would had invoked that.
Confused ??
I also think that way. Probably this would help.
A superclass var. refering to a subclass can only refer to those methods of the subclass which have been inherited by the subclass from the superclass.
Hope that helps.
[ May 15, 2003: Message edited by: Anupam Sinha ]
Sadhi Mushrif
Greenhorn

Joined: May 09, 2003
Posts: 11
Thanks for the help.
I got the point that since private is not accessible it is not inherited.
But I have some doubt from Anupama's code , is it that the method which will be called is ultimately dependent on the reference
variable type(Superclass) and not the type of the object it is referring to(Subclass).Only if Subclass inherits a method from Superclass
can a overridden method of Sub be called from Superclass reference.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Well firstof all my name is Anupam Sinha and not Anupama. The overridden method that will be invoked is decided at runtime and for overloaded methods it is decided at compile time. So now the simple rule is that a superclass reference var refering to subclass type will call the subclass method if the subclass inherits it. In simple terms you can say that it will be simply the superclass's method in case the subclass doesn't overrides it. But I guess what actually happens is when the subclass doesn't overrides it then the subclass inherits(if inheritable) the superclass method which is then called.
But what if as in your code the method can not be inherited then the method which the superclass has is executed.
But its not the other way round. That is a superclass reference var refering to subclass type can not refer to a method that the superclass does not have. That is you can not refer to subclass method using a superclass var type if the superclass does not has that method. The compiler will check to see at compiletime whether or not the method being refered to by the class type var is actually a method of that class.
I guess that should help.
As for your question "can a overridden method of Sub be called from Superclass reference. "
I guess overriden is the wrong word it should be overriding. If that is what you meant the answer is yes infact only an overriding method of Subclass be called from Superclass reference.
[ May 15, 2003: Message edited by: Anupam Sinha ]
Sadhi Mushrif
Greenhorn

Joined: May 09, 2003
Posts: 11
Thanks Anupam Sinha , that really did help
And I hope I got your name right this time, sorry about the misspelling your name earlier.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overriding private method
 
Similar Threads
Access control
Overriding
overriden method
Question on Overriding
Overiding