Java
Java Classes
Programming Concepts
Class.isInstance
Class.isAssignableFrom

java Class.isInstance vs Class.isAssignableFrom

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Class.isInstance and Class.isAssignableFrom are related, but they answer different questions. One checks whether a runtime object is an instance of a type, while the other checks whether one class can be assigned to another class reference.

isInstance Checks an Object

Use isInstance when you have an actual object reference and want to ask whether it matches a type.

java
1class Animal {}
2class Dog extends Animal {}
3
4public class Demo {
5    public static void main(String[] args) {
6        Object value = new Dog();
7
8        System.out.println(Animal.class.isInstance(value));
9        System.out.println(Dog.class.isInstance(value));
10        System.out.println(String.class.isInstance(value));
11    }
12}

This is conceptually similar to the instanceof operator, except the class on the left is itself dynamic data. That makes it useful in reflection-heavy code and frameworks.

If the value is null, isInstance returns false.

isAssignableFrom Checks Class Compatibility

Use isAssignableFrom when you have two Class objects and want to know whether a reference of one type can hold values of the other type.

java
1class Animal {}
2class Dog extends Animal {}
3
4public class Demo {
5    public static void main(String[] args) {
6        System.out.println(Animal.class.isAssignableFrom(Dog.class));
7        System.out.println(Dog.class.isAssignableFrom(Animal.class));
8    }
9}

The first call returns true because a variable of type Animal can refer to a Dog. The second returns false because a variable of type Dog cannot safely hold every Animal.

A useful way to read it is:

  • 'A.class.isAssignableFrom(B.class) means "can a B be assigned to an A reference?"'

That wording removes most of the confusion.

Compare the Two Side by Side

Here is the practical difference:

java
1class Animal {}
2class Dog extends Animal {}
3
4public class Demo {
5    public static void main(String[] args) {
6        Object dog = new Dog();
7
8        boolean objectCheck = Animal.class.isInstance(dog);
9        boolean classCheck = Animal.class.isAssignableFrom(Dog.class);
10
11        System.out.println(objectCheck);
12        System.out.println(classCheck);
13    }
14}

These both print true, but they answer different questions.

  • 'isInstance asks about the runtime object dog'
  • 'isAssignableFrom asks about the relationship between Animal.class and Dog.class'

That distinction matters in reflection code, dependency injection, serialization libraries, and plugin systems.

Interfaces Make the Difference Clearer

Interfaces often make the semantics easier to see.

java
1interface Flyer {}
2class Bird implements Flyer {}
3
4public class Demo {
5    public static void main(String[] args) {
6        Object value = new Bird();
7
8        System.out.println(Flyer.class.isInstance(value));
9        System.out.println(Flyer.class.isAssignableFrom(Bird.class));
10    }
11}

Both calls return true, but one looks at the object and the other looks at the type relationship.

If all you have is a runtime object, use isInstance. If all you have are Class objects, use isAssignableFrom.

When to Prefer instanceof

If the type is known at compile time, instanceof is often the clearest tool.

java
if (value instanceof Dog) {
    System.out.println("dog");
}

Reach for Class.isInstance mainly when the class is not hard-coded and comes from configuration, reflection, or a registry.

Common Pitfalls

The biggest pitfall is reading isAssignableFrom backward. Animal.class.isAssignableFrom(Dog.class) is true, but the reverse is false.

Another issue is using isAssignableFrom when you actually have an object and just want an instance check. In that case, isInstance or instanceof is more direct.

Developers also forget that isInstance(null) returns false, which can matter in validation code.

Finally, do not overcomplicate normal code with reflection methods when a direct instanceof or ordinary assignment already expresses the intent clearly.

Summary

  • 'Class.isInstance checks whether a runtime object matches a type.'
  • 'Class.isAssignableFrom checks whether one class can be assigned to another reference type.'
  • Read A.class.isAssignableFrom(B.class) as "can a B be assigned to an A variable?"
  • Use instanceof when the target type is known at compile time.
  • Most confusion comes from reversing the direction of isAssignableFrom.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.