• 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

Overriden Method Query

 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry,not return type access modifier
 
vidhya suvarna
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks alot Bob and Pranav ..i am now clear with it.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic