Class.isInstance does what you want.

if (Point.class.isInstance(someObj)){
    ...
}

Of course, you shouldn't use it if you could use instanceof instead, but for reflection scenarios it often comes in handy.

Answer from gustafc on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › core java › java instanceof operator
Java instanceof Operator | Baeldung
May 11, 2024 - In this quick tutorial, we’ll learn about the instanceof operator in Java. instanceof is a binary operator we use to test if an object is of a given type. The result of the operation is either true or false.
🌐
Programiz
programiz.com › java-programming › instanceof
Java instanceof (With Examples)
We can use the instanceof operator to check if objects of the subclass is also an instance of the superclass. For example, // Java Program to check if an object of the subclass // is also an instance of the superclass // superclass class Animal { } // subclass class Dog extends Animal { } class ...
🌐
DataCamp
datacamp.com › doc › java › instanceof
instanceof Keyword in Java: Usage & Examples
The instanceof keyword in Java is a binary operator used to test whether an object is an instance of a specific class or implements a particular interface.
🌐
IONOS
ionos.com › digital guide › websites › web development › java instanceof operator
How the Java instanceof operator works
October 22, 2024 - When you use instanceof in Java, the operator compares a reference variable with a specific class that is also specified by the user. It doesn’t include any ad­di­tion­al in­for­ma­tion about the nature of the object or the class.
🌐
Armedia
armedia.com › home › blog › “instanceof”, why and how to avoid it in code
Java “instanceOf”: Why And How To Avoid It In Code - Armedia
November 23, 2019 - The java “instanceof” operator is used to test whether the object is an instance of the specified type (class or subclass or interface). It is also known as type comparison operator because it compares the instance with type.
🌐
Simplilearn
simplilearn.com › home › resources › software development › understanding the use of instanceof in java
Understanding the Use of Instanceof in Java with Examples
September 14, 2025 - Instanceof in java is a binary operator used to check if the specified object is an instance of a class, subclass, or interface. Learn all about it now!
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
Find elsewhere
🌐
Cornell Computer Science
cs.cornell.edu › courses › JavaAndDS › files › instanceof.pdf pdf
Instanceof and getClass() ÓDavid Gries, 2018
Java has two ways to determine what class an object is: operator instanceof and function getClass().
🌐
Medium
medium.com › @AlexanderObregon › how-javas-instanceof-operator-works-09071a27cd3b
How Java’s instanceof Operator Works | Medium
February 28, 2025 - The instanceof operator in Java checks if an object belongs to a specific type at runtime. It plays a big part in type checking, especially when dealing with inheritance and interfaces.
🌐
GeeksforGeeks
geeksforgeeks.org › java › instanceof-keyword-in-java
instanceof Keyword in Java - GeeksforGeeks
July 23, 2025 - ((child_class_name) Parent_Reference_variable).func.name() When we do typecasting, it is always a good idea to check if the typecasting is valid or not. instanceof helps us here. We can always first check for validity using instanceof, then do typecasting. ... // Java program to demonstrate that non-method // members are accessed according to reference // type (Unlike methods which are accessed according // to the referred object) class Parent { int value = 1000; } class Child extends Parent { int value = 10; } // Driver class class Test { public static void main(String[] args) { Parent cobj = new Child(); Parent par = cobj; // Using instanceof to make sure that par // is a valid reference before typecasting if (par instanceof Child) { System.out.println( "Value accessed through " + "parent reference with typecasting is " + ((Child)par).value); } } }
🌐
Jenkov
jenkov.com › tutorials › java › instanceof.html
Java instanceof operator
July 4, 2020 - The Java instanceof operator can determine whether a given object is an instance of a given class or interface. The Java instanceof operator was updated in Java 14 with pattern matching functionality, making it more concise to use.
🌐
W3Schools
w3schools.com › java › ref_keyword_instanceof.asp
Java instanceof Keyword
The instanceof keyword checks whether an object is an instance of a specific class or an interface. The instanceof keyword compares the instance with type. The return value is either true or false.
🌐
Baeldung
baeldung.com › home › java › core java › class.isinstance vs class.isassignablefrom and instanceof
Class.isInstance vs Class.isAssignableFrom and instanceof | Baeldung
November 13, 2025 - The instanceof keyword is a binary operator, and we can use it to verify whether a certain object is an instance of a given type. Therefore, the result of the operation is either true or false.
Top answer
1 of 9
3

If you want to add a method to a class hierarchy without actually adding the method, consider the Visitor Pattern. You could create a validation visitor, and let each entity select the appropriate method of the visitor.

First, your ParentEntity class hierarchy would need a bit of boilerplate to support visitors:

interface EntityVisitor<T> {
  T visitA(AEntity a);
  T visitB(BEntity b);
}

class ParentEntity {
  <T> T accept(EntityVisitor<T> v);
}

class EntityA extends ParenEntity {
  ...
  @Override <T> T accept(EntityVisitor<T> v) {
    return v.visitA(this);
  }
}

Next, we can implement and use a visitor that performs validation.

class Validation implements EntityVisitor<Void> {
  EntityRepository repository;
  ...
  @Override Void visitA(AEntity a) { ... }
  @Override Void visitB(BEntity b) { ... }
}

class EntityRepository ... {
  void save(List<ParentEntity> list) {
    list.ForEach(e -> {
      e.accept(new Validation(this));
      ...
    });
  }
}

The validation visitor can have access to both the entity and the repository (in order to make further queries), and will therefore be able to perform the full validation.

Using such a pattern has advantages and disadvantages compared to an instanceof check and compared to moving the validation logic into the entities.

  • An instanceof is a much simpler solution, especially if you only have very few entity types. However, this could silently fail if you add a new entity type. In contrast, the visitor pattern will fail to compile until the accept() method is implemented in the new entity. This safety can be valuable.

  • While this pattern ends up having the same behaviour as adding a validate() method to the entities, an important difference is where that behaviour is located and how our dependency graph looks. With a validate() method, we would have a dependency from the entities to the repository, and would have referential integrity checks intermixed with actual business logic. This defeats the point of an Onion Architecture. The visitor pattern lets us break this dependency and lets us keep the validation logic separate from other business logic. The cost of this clearer design structure is extra boilerplate in the form of the EntityVisitor interface and the accept() method that must be added to all entities in the relevant class hierarchy.

Whether these trade-offs are worth it is your call. You know your codebase best, and you have the best idea how it might evolve.

However, performing validation based on the result of multiple queries can lead to data integrity problems. The repository should either make sure to use database transactions (and offer an API that clearly communicates when modifications have been committed), or the relevant integrity checks should be done within the database, e.g. using constraints in an SQL database. In some cases, the validation checks can also be expressed as part of an insert or update query.

2 of 9
2

I know what I am about to answer is not exactly good practice but if you want to avoid instanceof's and have a generic way to call the respective method for that subclass you could use reflection:

Method m = EntityRepository.class.getMethod("validate", e.getClass());
m.invoke(this, e);

Of course, this will have a negative effect on performance and in some ways maintainability (with the only upside being less code).

Regarding the performance overhead, you can somewhat mitigate it by loading all the methods at startup:

Map<Class<?>, Method> methods = new HashMap<>();
Reflections reflections = new Reflections(ParentEntity.class, new SubTypesScanner());
Set<Class<? extends Animal>> subclasses = reflections.getSubTypesOf(ParentEntity .class);
for (Class<?> c : subclasses) {
    methods.put(c, Solution.class.getMethod("makeTalk",c));
}

Where Reflections comes from the Reflections library

And then just call the method using:

methods.get(e.getClass()).invoke(this, e)
🌐
Medium
hongjae.medium.com › the-difference-between-instanceof-vs-class-isassignablefrom-in-java-aecbf4e4e863
The difference between instanceof VS Class.isAssignableFrom in Java | by Jaehyun Hong (Jay) | Medium
April 26, 2024 - Class.isAssignableFrom must perform calculations to determine whether a given class is a superclass or interface of another class. 2. instanceof is more flexible. instanceof can check if an object is an instance of a particular class or interface.
🌐
How to do in Java
howtodoinjava.com › home › java object oriented programming › java instanceof operator
Java instanceof Operator - HowToDoInJava
January 3, 2023 - Java instanceof operator (also called type comparison operator) is used to test whether the specified object is an instance of the specified type (class, subclass, or interface).
🌐
GeeksforGeeks
geeksforgeeks.org › java › instanceof-operator-vs-isinstance-method-in-java
instanceof operator vs isInstance() Method in Java - GeeksforGeeks
January 3, 2022 - The instanceof operator and isInstance() method both return a boolean value. isInstance() method is a method of class Class in java while instanceof is an operator.
🌐
Scaler
scaler.com › home › topics › what is instanceof java operator?
What is Instanceof Java Operator? - Scaler Topics
January 3, 2023 - In Java, the instanceof operator is used to determine whether an instance belongs to a particular type (class, subclass, or interface).