This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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: 573
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: 573
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: 573
Thanks for the reply.
Tony
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what should super.getClass() return
 
Similar Threads
ClassCastException on Down Casting
ClassCastError vs. Compilation Error
replacement for alternative design pattern
Please somebody explain
Please help me out with this program