This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes accessing subclass name in superclass Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "accessing subclass name in superclass" Watch "accessing subclass name in superclass" New topic
Author

accessing subclass name in superclass

Anoop Pillai
Greenhorn

Joined: Oct 05, 2008
Posts: 9
Hi,
I have a superclass which is extended by 'N' number of classes. Now I have modified a method in the superclass to get the name of the calling subclass.
Is there any way through which my superclass method shall get its subclasses class name?

(Note: I can't modify my subclasses code; I can only modify the existing method written in the superclass which is called by all other subclasses.)

For Ex:



Here when class B,C & D is executed, it calls the super class method displayClass() and it should print the name of the respective class which is calling the method i.e., Class B,C & D respectively.

Can anybody tell me how will I achieve this in the superclass method displaClass() without overriding the method in the subclass or passing anything as argument to the superclass method i.e., without touching the code in the subclasses?

Thanks in advance.


The Next Best Thing To Knowing Something Is Knowing Where To Find It.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Hi,

Welcome to JavaRanch!

The method getClass(), inherited by every class from java.lang.Object, returns the Class object representing the object on which the method is called. The Class object has a getName() method that returns the name of the class. So your displayClass() method can call getClass(), and then getName() on the Class object, to get the name of the class of the object it finds itself in.


[Jess in Action][AskingGoodQuestions]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
As EFH has told you, that should work nicely. It is an example of polymorphism; you might not have overridden the displayClass() method, but it still takes details from the object it is invoked on.
Anoop Pillai
Greenhorn

Joined: Oct 05, 2008
Posts: 9
Thanks all for the help..
I was trying to call displayClass() using the super class object in the subclasses.
For Ex:
//in class C
A obj=new A();
obj.displayClass();

instead of calling it as
C obj=new C();
obj.displayClass();

that's why it was not working for me...


thanks all for replying.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
You mean

A ac = new C();
ac.displayClass();

?
Vinod K Singh
Ranch Hand

Joined: Sep 30, 2008
Posts: 198
Originally posted by Anoop Pillai:
Thanks all for the help..
I was trying to call displayClass() using the super class object in the subclasses.
For Ex:
//in class C
A obj=new A();
obj.displayClass();

instead of calling it as
C obj=new C();
obj.displayClass();


Thats a wrong way to call super class method. You should simply do-

From your subclass method.


My Blog
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: accessing subclass name in superclass
 
Similar Threads
Super and subclass
static method overriding
this and super
Overriding private functions
operator instanceof confusion