Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting child class name from parent class

 
Venkatesh Rajendran
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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.
Venkat
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can have Class A keep up with all created instances in a static collection. Something like:

Here's the output:
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Layne
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What Layne said... plus: in any non-static method of A, if you call:

this.getClass().getName()

you will receive the name of the instantiated class (B, C or D).
hth,
bear
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Venkat
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic