This week's book giveaway is in the Big Data forum.
We're giving away four copies of Elasticsearch in Action and have Radu Gheorghe & Matthew Lee Hinman on-line!
See this thread for details.
The moose likes Java in General and the fly likes Getting child class name from parent class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Getting child class name from parent class" Watch "Getting child class name from parent class" New topic

Getting child class name from parent class

Venkatesh Rajendran
Ranch Hand

Joined: May 17, 2001
Posts: 59
How can I get child class name from parent class.. for example I have one class called Class A,B,C and D
public class B extends A{
public class C extends A{
public class D extends A{
Now from A class I want to know which class has been called. Is there any way to know that?
It would be great if any one helps me on this.
Thanks in advance.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
You can have Class A keep up with all created instances in a static collection. Something like:

Here's the output:

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
My first question is why do you want to do this? It seems to defeat the purpose of inheritence and the built-in polymorphism mechanism. When dealing with class hierarchies, you should strongly rely on polymorphism when it is appropriate.
Perhaps if you explain what you need to do, we can suggest an approach that more closely follows the OOP paradigm.

Java API Documentation
The Java Tutorial
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 62145

What Layne said... plus: in any non-static method of A, if you call:


you will receive the name of the instantiated class (B, C or D).

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
I suspect LL and BB's answers are most appropriate to the question. MM's approach is useful if you want to be able to access all instances that have been created, rather than just talk about a particular instance. A couple corrections though:

There's no opint in synchronizing on "this". Inside the constructor, no other thread can possibly have a reference to the same "this" object that's being constructed - the reference won't be made available until the constructor completes. (Well, unless you intentionally pass a reference to another thread from within the constructor, but you'd have to be kind of insane to do that.) What you do need to synchronize here is the access to the List "children", which is shared by all instances and thus may be accessed concurrently:

Also it's necessary to synchronize any other access to the List:

The getChildren() method is problematic - it should probably be synchronized, but more importantly any subsequent use of the List should be synchronized. Or we could just return a copy, so we don't have to worry about subsequent synchronization:

"I'm not back." - Bill Harding, Twister
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
You're right on the synching of this in the constructor. I just kind threw the code together without a lot of thought. It should also be synched in the getChildern() method. One other potential problem with this code is a memory leak. You really need a way to remove the objects from the Set when they have no other outside references. A transparent way would be to wrap each object in a WeakReference.
Venkatesh Rajendran
Ranch Hand

Joined: May 17, 2001
Posts: 59
Thanks for your valuable answers, here is my exact requirement,
As I have I have one base class called A, there are so may classes going to extends the class. I have one properties file with following content
ChildClassName = Some message
From my parent class I want to read the appropriate key and value pair for the dereived class.
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

Here's my solution to the same problem. (Note that this code uses my helper classes, but you can follow along with the logic.):

My Environment class provides access to Properties files; the StringFunctions class just is a helper class. In this case it returns the last token in the fully-qualified class name (just the Class Name, not the Package Name. You can use the package too, if you want; I just don't want ).
So what I'm dong here is first looking for the value for the derived class. If it can't find it, it uses the default setting for the base class (which is a hard-coded key name here).
Hope that this logic is clear.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
As I have I have one base class called A, there are so may classes going to extends the class. I have one properties file with following content
What exactly are you doing with the property file? Are the child objects already created when the parent reads the file? Do you want to create child objects based on the contents of the file? That would imply the need of a Factory class to create the children. Give us a little more info about your overall architechture.
Bob Sullivan

Joined: May 13, 2003
Posts: 5
It sounds like there is an inherited method in the superclass that acts on behalf of any subclass and the behavior is intended as if it were implemented in the subclass. There should be no problem if you ask in the superclass for this.getClass.getName(). Lets say you have the following structure: Car (superclass) RedCar, BlueCar (subclasses). If you have an instance of BlueCar and a method implemented only in Car asks this.getClass().getName() you will get "BlueCar".
wood burning stoves
subject: Getting child class name from parent class