This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding

 
Winson Chow
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Parent {
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:
A. Compile error can't override the private method print()
B. From Parent
From chield
C. From Parent
From Parent
D. From chield
From chield
Given ans : C
I don't know why the ans is C instead of B.
 
Ramesh Donnipadu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winson,
Take another look at print() method of class parent. It is declared as a private method. Private methods are implicitly final and can't be overridden. So there is no dynamic lookup.
 
Viji Bharat
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramesh:
I tried this program and yes the answer is C though I also thought it was B. Could you please explain how the binding is done? It is not clear to me.

Parent P1 = new Parent();

here you have the reference P1 pointing to object Parent. when P1.print() is called now, it prints "From Parent". This is fine.
Now, when you say P1 = new Chield();, isn't P1 referring to object Chield? In which case when P1.print() is called, should it not be calling Chield's print? which is the only print() method available in Chield? (since print() in Parent is private, it is not visible in Chield). I am not clear about how binding is done in this case. If you get a chance or if anyone has a good explanation, kindly let us know.

[This message has been edited by Viji Bharat (edited September 29, 2000).]
 
pete hesse
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramesh,
"Private methods are implicitly final and can't be overridden. "
In the code example, the method appears to be overridden, but the compiler treats it as a separate method because the method of the parent is private, and allows it. However, if the method were declared 'final' in the parent, the compiler would complain about an attempt to override a final method.
So I don't think you can say that private methods are implicitly final. Just an fyi...
 
Ramesh Donnipadu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Pete for correcting on that. Yes. final is different from being private.
Viji,
The question of dynamic binding arises only in case of overriding scenario. In this case there is no overriding and hence the symbols are resolved at compile time (static binding). In case of static binding, I don't think compiler looks at object for tying method invocation with method definition.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic