-3
public static void main(String[] args)
{
    Class b = B.class;
    System.out.println(b.isAssignableFrom(A.class));
}
static abstract class A{

}
static class B extends A{

}

Output:

false

The output is false I try to use instanceof and it has compile errors please help

Edit is solved you need to compare baseclass is assignable from extended pretty stupid that you can't use is class instanceof abstract anyways

joseph lake
  • 53
  • 1
  • 7
  • 3
    There are no generics here. And the result is correct, you cannot assign to a `B` from an `A`. – Oliver Charlesworth Mar 05 '18 at 20:21
  • 1
    Please **read the documentation**, i.e. the javadoc of [`isAssignableFrom(Class> cls)`](https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#isAssignableFrom-java.lang.Class-), which states: *"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"*. Since `B` is not a super of `A`, method returns `false`. --- http://idownvotedbecau.se/noresearch/ – Andreas Mar 05 '18 at 20:24
  • If you wanted the equivalent of `b instanceof A`, then reverse the call: `A.class.isAssignableFrom(b)` – Andreas Mar 05 '18 at 20:27
  • Possible duplicate of [How do I find out if generic is instanceof a class in Java?](https://stackoverflow.com/questions/19642586/how-do-i-find-out-if-generic-is-instanceof-a-class-in-java) – Jose Da Silva Gomes Mar 05 '18 at 21:09

1 Answers1

2

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.

Community
  • 1
  • 1
Zabuzard
  • 25,064
  • 8
  • 58
  • 82
  • "Can I assign an Animal (A) to a Dog (B)?" Well since B extends A it should be returning true I think java is wrong here. Because dog is an animal and it can be casted back to an animal. For example if they were both objects I could say is dog instanceof animal and it would return true then I say is dog assignable to animal says false. Java is definitely wrong there. Now if you said cat is assignable to dog then it should return false but, not if it extends it – joseph lake Mar 05 '18 at 20:37
  • Yeah I just noticed it says from. They should have class.isInstanceof() or fix the compiler to allow for if(classObject instanceof AbtractClass) { //do code } – joseph lake Mar 05 '18 at 20:45
  • The logic is reversed to what you think. The method asks the question the other way around. It asks if an animal can be assigned to a dog. Or, equivalently if a dog is assignable **from** an animal. – Zabuzard Mar 05 '18 at 20:45
  • Well, there is [Class#isInstance(Object)](https://docs.oracle.com/javase/9/docs/api/java/lang/Class.html#isInstance-java.lang.Object-). It returns the same as the `instanceof` operator. So you can do `Animal.class.isInstance(dog)` and also try `Dog.class.isInstance(animal)` which would return `true` if the animal is of type `Dog`, like you could do `if (dog instanceof Animal)` or `if (animal instanceof Dog)`. – Zabuzard Mar 05 '18 at 20:46