• 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

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Right! We're on it! Let's get to work tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic