wood burning stoves 2.0*
The moose likes Java in General and the fly likes what should super.getClass() return Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "what should super.getClass() return" Watch "what should super.getClass() return" New topic
Author

what should super.getClass() return

Tony Evans
Ranch Hand

Joined: Jun 29, 2002
Posts: 576
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: 576
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: 576
Thanks for the reply.
Tony
 
wood burning stoves
 
subject: what should super.getClass() return