Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question about polymorphism

 
zhu weitao
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The result is

Parent's method2
Parent's method1

but if change the method method1 in class Parent to default protected or public ,the result is

Parent's method2
Child's method1

why???
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As your code stands method1() is not inherited to Child class so it's not an override in the Child class. So Parent class methods called. But if you make the modifier default/public/protected of method1() in Parent then the method inherited so overriden by the Child class. Hence method1() of Child class called.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vijitha Kumara:
As your code stands method1() is not inherited to Child class so it's not an override in the Child class.


As its not inherited to child class , and we had defined a method1(), in Child class, so the code must executes its own method1(), printing

Child's method1


I`m confused with above flow ? Pl elaborate it more !
 
zhu weitao
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know method1() is not inherited to Child class , but the class Child has the method1() method itself , why doesn't it invoke it's method1() method
 
Rob Spoor
Sheriff
Pie
Posts: 20526
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you are not overriding method1. Overriding and polymorphism are not applicable for private methods. So instead of overriding method1, you are creating a new method that just happens to have the same signature as the private method.

If you would make method1 protected in Parent you would get what you expect.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Prime:
So instead of overriding method1, you are creating a new method that just happens to have the same signature as the private method.


Ok, I agree !

Now, this what compiler saw ,



Now, here object 'p' is of type 'Child' , so it gonna call method2 at line 1.. which in turn calls method1 at line 2. Now we have method1 body in Child class , which prints "Child's method1"..

This is how I conceive it ?

Where I`m wrong ?
[ September 17, 2008: Message edited by: Sagar Rohankar ]
 
Rob Spoor
Sheriff
Pie
Posts: 20526
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you are overriding method2 as well it will work. That's because Child's method2 does not know anything about Parent's method1 and call Child's method1.
 
lahiru dharmasena
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Rob Prime,

why is that doesn't happen when parents method1 is private?

 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still confused !

Actually , I want to say like The compiler insert this code in Child class, which is inherited from Parent class..



Now this method2 code is inserted by compiler with call to method1() in it and in Child class we have our own implementation of method1(), which prints "Child's method1"..

So How the original code works ?
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case there is no access to a private method from the subclass. If you have a method of the same name, the compiler has to ignore the previous method.

Remember a private method is entirely for "internal consumption;" it is only for use inside its own class. When you create a new method with the same name, the compiler sees that as something completely new and independent.
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sagar Rohankar:
Now this method2 code is inserted by compiler with call to method1() in it


I'm not sure about that the compiler inserts that part in to child code or at the runtime it goes for the Parent object and execute method2() when it sees that child doesn't override that method. In that case Parent's method1() will be executed since it can't access the private method of Child from there (This is what Campbell Ritchie means i think). Correct me if I'm wrong.
 
lahiru dharmasena
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case Parent's method1() will be executed since it can't access the private method of Child from there


so, your saying look up is comes from parent to child. first go to the parent and look if the method can override the child's method. if it can override, use child's method if it can't, use parent's method. mmm... according to my knowledge it's other way around. (correct me if i'm wrong)

first, look on child's class and if not found go to the parent.

 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, What I said ,it goes for the Parent object and execute method2() when it sees that child doesn't override that method, Not it comes back again to child class.
 
lahiru dharmasena
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok. i got it now. thanks
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At runtime , when I checked thy type of object 'p', using Object#getClass(), which returns class Child..

Now , Child class must contain definition of 'method2()', in order to execute ! But there is no method defined as such , but it is inherited from Parent class !!.

Now this code contains the call to method1(), the object is of type 'Child' , so the this code must get execute ..



To simplify , I rewrite the code as :


Output :
Type of p : class Child
I'm executing method2 class Child
Parent's method2
Parent's method1 // I'm expecting " Child's method1"


Where I`m wrong ?

May be I'm missing something silly or any simplest specification !
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vijitha Kumara:
No, What I said ,it goes for the Parent object and execute method2() when it sees that child doesn't override that method, Not it comes back again to child class.


If that's How overridden methods call gets executed , then I got it !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic