Hi I am trying to find out the subclass, form an superclass object.
if Class Super is the super class.
and Class Sub1 and Class Sub2 extends Class Super.(all classes are public)
Lets say I have a object of Super type as
Super superObject = new Sub1();
now for the superObject, is it possible to find which subclass the superObject extends in Java?
Since “SuperClass will not be aware of any SubClasses it has”, can you please tell me is my above question valid in the first place, or am I missing any basic concept?
Thanks in advance.
The getClass() method will return the concrete type of the object at runtime, not the reference type. So simply doing:
Will give you Sub1.class It’s not clear from your question what you then want to do with it. You can simply call getName() on it, check if some other reference you have is of the same type, etc etc.