• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Overriding private method

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...........
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Anupam Sinha , that really did help
And I hope I got your name right this time, sorry about the misspelling your name earlier.
 
reply
    Bookmark Topic Watch Topic
  • New Topic