Your class B
extends from A
, not the other way around. Imagine a more concrete example like
public abstract class Animal { ... }
public class Dog extends Animal { ... }
public class Cat extends Animal { ... }
The result of B.class.isAssignableFrom(A.class)
is correctly false
since you are asking
Can I assign an Animal
(A
) to a Dog
(B
)?
Which is not possible in general since there can be different animals like the Cat
.
Animal animal = new Cat();
Dog dog = (Dog) animal; // Will not work since animal is a cat
For more details see the documentation of the method:
Determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter. It returns true
if so; otherwise it returns false
. If this Class object represents a primitive type, this method returns true
if the specified Class parameter is exactly this Class object; otherwise it returns false
.
Specifically, this method tests whether the type represented by the specified Class parameter can be converted to the type represented by this Class object via an identity conversion or via a widening reference conversion. See The Java Language Specification, sections 5.1.1 and 5.1.4, for details.