Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

accessing subclass name in superclass

 
Anoop Pillai
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48435
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48435
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean

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

?
 
Vinod K Singh
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic