aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriden Method Query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overriden Method Query" Watch "Overriden Method Query" New topic
Author

Overriden Method Query

vidhya suvarna
Ranch Hand

Joined: Aug 28, 2008
Posts: 148
Source: http://certification.about.com/library/quiz/java/blscjp14_q28.htm

What is displayed when the following is executed?

class Parent{
private void method1(){
System.out.println("Parent's method1()");
}
public void method2(){
System.out.println("Parent's method2()");
method1();
}
}
class Child extends Parent{
public void method1(){
System.out.println("Child's method1()");
}
public static void main(String args[]){
Parent p = new Child();
p.method2();
}
}

The answer for the above code seems to be :
Parent's method2()
Parent's method1()

I didnt get how method1() of Parent class is invoked from the method2() when the reference is an object of Class Child.

Th explanation provided was that the method1() of Parent was PRIVATE if it had been public/protected/default then Child Class method1() will be invoked.
Can anyone explain to me how is it so?


SCJP 1.4 - 88%<br />SCWCD 1.5 - Preparing
SaiRishi Anusuri
Greenhorn

Joined: Mar 18, 2008
Posts: 14
the answer is correct .
here method1() is marked private.
to override the return type and arguments should be unchanged
here he return type differs.e object is subtype

Parent p =new child();
the reference type is superclass
and the object is subclasstype the superclass method will be invoked.
vidhya suvarna
Ranch Hand

Joined: Aug 28, 2008
Posts: 148
SaiRishi,
subclass Child is overridding the method1 of Super class..the return type(void) is same..only thing that differs is the access modifier.
But for overridding the access modifier can less restrictive..so subclass method1() has access modifier as PUBLIC..

For overridden methods, object type determins which method to be invoked..its a decision at runtime.
method2() of superclass is invoked only because there's no method by that name in the subclass..
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
The way I see this one is like this:

p.method2();

is a call of method2() using a Parent reference that refers to a Child object. Since Child did not implement a method2() the only one that we have was inherited from Parent, so it gets called and we get the printout
"Parent's method2()". After the print a call is made to method1(). The question is, which one? Well, the method1() in Parent is private so Child can NOT inherit it. However, Child has implemented a method1() with the same signature, same return type, but it is marked public. But be very clear.... this is NOT overriding. You can not override a private method.

So what decides which version gets called? The type of the reference variable that we used to call in with which, in our case, is Parent. Therefore we wind up calling the Parent version of method1() printing out "Parent method1()".


UPDATE

I should have prototyped, THEN commented.

Apparently, when the method1() call is within the suprclass method2() it will always call the parent method1().

I prototyped your test and added a Child c = new Child(); and c.method2(); and it still calls Parent method1() on both lines.

HOwever, when I then added a c.method1() directly, it called the child version.

My bad.
[ September 09, 2008: Message edited by: Bob Ruth ]

------------------------
Bob
SCJP - 86% - June 11, 2009
Pranav Bhatt
Ranch Hand

Joined: Mar 20, 2006
Posts: 284
Vidhya,
Private methods are not inherited, so you're not overriding it also. So here method1() of base class will be called as dynamic call on basis of object wont be put up as overriding is not taking place. Try changing your code as below and run it again, you will see the difference.

class Child extends Parent{
public void method1(){
System.out.println("Child's method1()");
}
public void method2(){
System.out.println("Child's method2()");
method1();}

public static void main(String args[]){
Parent p = new Child();
p.method2();
}
}



Hope this helps

[ September 09, 2008: Message edited by: Pranav Bhatt ]
[ September 09, 2008: Message edited by: Pranav Bhatt ]
SaiRishi Anusuri
Greenhorn

Joined: Mar 18, 2008
Posts: 14
sorry,not return type access modifier
vidhya suvarna
Ranch Hand

Joined: Aug 28, 2008
Posts: 148
Thanks alot Bob and Pranav ..i am now clear with it.
Rasto Slivensky
Greenhorn

Joined: Aug 20, 2008
Posts: 4
Agree with Bob and Pranav. The way I'm looking at this:

since method1() wasn't overriden (remember it's private), call method1(); in Parent's method2() is solved at compile-time. So whenever method2() is run it'll use method1() from Parent (in this case doesn't matter if the object type != reference type).

But when you change the access modifier of Parent's method1() to anything else, it will render the Child's method1() as override and hence the compiler will not be able to determine which method to call. That's why it creates an table of methods (in our case Parent's method1() and Child's method1()) and the decision which one to run is made during the run-time based on the object type and the methods in the table. (so in this case the object type matters)

Hope it helps to understand. It helped me at least And thanks to Vidhya for good example.
 
Consider Paul's rocket mass heater.
 
subject: Overriden Method Query