aspose file tools*
The moose likes Beginning Java and the fly likes question about polymorphism Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "question about polymorphism" Watch "question about polymorphism" New topic
Author

question about polymorphism

zhu weitao
Greenhorn

Joined: Sep 02, 2008
Posts: 9


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

Joined: Mar 24, 2008
Posts: 3825

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.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

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 !


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
zhu weitao
Greenhorn

Joined: Sep 02, 2008
Posts: 9
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

Joined: Oct 27, 2005
Posts: 19672
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

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

Joined: Oct 27, 2005
Posts: 19672
    
  18

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

Joined: Sep 17, 2008
Posts: 6
hi Rob Prime,

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

Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

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

Joined: Oct 13, 2005
Posts: 38412
    
  23
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

Joined: Mar 24, 2008
Posts: 3825


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

Joined: Sep 17, 2008
Posts: 6
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

Joined: Mar 24, 2008
Posts: 3825

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

Joined: Sep 17, 2008
Posts: 6
ok. i got it now. thanks
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

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

Joined: Feb 19, 2008
Posts: 2902
    
    1

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 !
 
Don't get me started about those stupid light bulbs.
 
subject: question about polymorphism