Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what should super.getClass() return

 
Tony Evans
Ranch Hand
Posts: 598
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 598
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 598
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.
Tony
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic