• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

accessing subclass name in superclass

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79405
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79405
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You mean

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

?
 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic