• 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

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

i need your help, i don't understand the output of this code. Can you help me please ?

<CODE>
class Parent
{
private void method1() // the method is private
{
System.out.println("Parent's method1()");
}
public void method2()
{
System.out.println("Parent's method2()");
System.out.println(this);
method1();
}
}

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

I have this as output in my computer :

Parent's method2()
Test@18d107f
Parent's method1()

I think, it shoul be :

Parent's method2()
Test@18d107f
<BOLD> Test's method1() </BOLD> because the object executing method2() is Test@18d107f

Thank you very much.

Armel
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Armel:

Your output is well. You have declared your reference variable p as type Parent, but Test. That means that your reference variable only knows the Parent�s behaviors, if you want the Test�s behavior (means methods) you must down-cast it or declare your reference p as Test type. Read about polymorphism.

Do you get it?

Regards,
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because Test doesn't override method2() your code is running the method defined in class Parent, and that method is calling method1() also defined in Parent.

The over-riding rules aren't being applied here as you would expect. If you had over-ridden method2() in Test then at runtime the JVM would have executed the method2() in Test.
 
Alejandro Galv�n
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi:

Ian:
Be careful, method2() is inherited by Test class, it�s not the reason.

Armel:
Maybe I wrote wrong, the output given by your JVM is correct, not the one you think.

Thanks,
 
Armel Moukoss
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Alejandro for your help, but i am still confused. I would be glad to receive others helps. Thank you.

Armel
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Armel et al.,
First you have wrongly used code tags, they must be in square brackets and not angle brackets.
Ok lets try to answer your question now.
Parent class has couple of methods method1() and method2(). method1() is private and hence it will not be inherited by subclasses and method2() since its not marked private it will be inherited by subclass and can be overriden by subclass. Here you have chosen not to override method2() in Test class. Now you have defined method1() in Test class, Because method1() is not inherited by Test class this method1() is jst a new method in your class and is not a legal override of method1() defined in Parent class.
In the example method1() is not inherited by subclass Test, but method2() is inherited by Test.
Parent p = new Test();
You create a Parent reference and assign a subclass object Test.
p.method2(); First you can access method2() from Test class because its not marked private and hence its inherited by Test class.Since you are not overriding method2() , method2() of Parent class is invoked. Within method2() of parent class you invoke method1() and since method1() is not inherited by Test class [because its marked private and therefore cannot be overriden] method1() of Parent class is invoked.

Modify Parent.method1() to public. and you will observe the results Or you can override method2() in your subclass and you will observe the result.

If a method() is inherited by subclass then invocation of method() from Parent class invokes method() of subclass at runtime if its not inherited and not defined in the class from which you are invoking it you will get a compiler error.
If a method is not inherited by subclass then invocation of method() from Parent class will invoke method() of Parent class provided its defined in Parent class.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Deepak,
Thanks for such an explanatory answer. But I could not understand the following lines :

If a method() is inherited by subclass then invocation of method() from Parent class invokes method() of subclass at runtime if its not inherited and not defined in the class from which you are invoking it you will get a compiler error.

If the method is not inherited by Subclass, then it will simply call the Super class's method but it will not throw compilation error. Please correct me if I am missing something.
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What if the method in super class is marked private in that case you cannot access that method outside the super class and hence it will throw compiler error.
 
Sagar Jani
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Deepak.
 
Sagar Jani
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Deepak.
 
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is my take on this:
Since 'method1' has been defined as private in the parent class, any call to parent's 'method1'(as is being done in parent's 'method2') is bound at compile time itself i.e. there is no scope left to resolve the method call at runtime based on the object through which it is invoked. Hence in the above code, parent's 'method1' is invoked even though the calling object is of child type and has its own 'method1'.
 
reply
    Bookmark Topic Watch Topic
  • New Topic