| Author |
what should super.getClass() return
|
Tony Evans
Ranch Hand
Joined: Jun 29, 2002
Posts: 521
|
|
This is just a tad confusing me. I have the following code. class derived1 extends Base { public void inSide() { System.out.println(this.getClass().getName()+" Super class is "+super.getClass().getName()); } } now when I call d1.inSide(); this.getClass().getName() returns "derived1" this is what I expect. super.getClass().getName() returns "derived1" this is not what I expect, I expected it to return Base, as Base is dervived1s super class. Very confused Tony
|
 |
Tony Evans
Ranch Hand
Joined: Jun 29, 2002
Posts: 521
|
|
If I use this.getClass().getSuperclass() then it will return the super class. What I am trying too is work out the rules for using super. I know if I want to call an overloaded base method I use super.methodName as opposed to this.methodName. But it looks as if super.objectMethod will return the dervide class rather than the super class, does anyone know the reason why.
|
 |
Don Kiddick
Ranch Hand
Joined: Dec 12, 2002
Posts: 580
|
|
From the javadocs, getClass returns the runtime class of an object. The runtime class of your object is "derived1". As you do not overide the getClass method, you execute the same method with both calls. T.
|
 |
Tony Evans
Ranch Hand
Joined: Jun 29, 2002
Posts: 521
|
|
Thanks for the reply. Tony
|
 |
 |
|
|
subject: what should super.getClass() return
|
|
|