File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding 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" Watch "Overriding" New topic


Winson Chow

Joined: Sep 29, 2000
Posts: 5
public class Parent {
private void print(){
System.out.println(" From Parent");
public static void main(String args[]){
Parent p1 = new Parent();
p1 = new Chield();
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

Joined: Sep 16, 2000
Posts: 100
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

Joined: Sep 18, 2000
Posts: 101
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

Joined: Aug 29, 2000
Posts: 44
"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

Joined: Sep 16, 2000
Posts: 100
Thanks Pete for correcting on that. Yes. final is different from being private.
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.
I agree. Here's the link:
subject: Overriding
It's not a secret anymore!