Check subclass of an object in Java

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.

Jon Skeet
people
quotationmark

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 a ClassCastException. Otherwise the result is false.

So in your case:

if (obj instanceof MinorClassA) {
    ...
}

people

See more on this question at Stackoverflow