If you call a.getClass() , then it always returns you an instance of the class from which this object was created. In your case, this is B , so it will return you B.class .
Now, if you want to call a method on a and get a class like A.class , you cannot do it the usual way.
One way would be to define a method in a
public static Class<A> getType() { return A.class; }
and then you can call a.getType() , which is equal to A.class . We use the static method here because they are not overridden.
source share