Consider the classes MinorClassA
and MinorClassB
, both extending MajorClass
. I have an object of MajorClass
that I know for sure is actually an instance of one of its subclasses.
MinorClassA subclassedObj = new MinorClassA();
MajorClass obj = subclassedObj;
//------ More code -------------
if( subclassof(obj) == MinorClassA) //Something like this
How can I find out what is the subclass of that object? I do not have access to subclassedObj
, only to obj
.
Edit for clarification: I know how to check if MinorClassA
is instance of MajorClass
, but not the other way around.
Sounds like you want the instanceof
operator:
At run time, the result of the
instanceof
operator is true if the value of the RelationalExpression is not null and the reference could be cast to the ReferenceType without raising aClassCastException
. Otherwise the result is false.
So in your case:
if (obj instanceof MinorClassA) {
...
}
See more on this question at Stackoverflow