You should use isAssignableFrom:

if (YourInterface.class.isAssignableFrom(clazz)) {
    ...
}
Answer from Flavio on Stack Overflow
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ core java โ€บ determine if a class implements an interface in java
Determine if a Class Implements an Interface in Java | Baeldung
January 8, 2024 - We can check this with the isAssignableFrom() method of the Class Object. This method returns a true if the object inherits the specified interface, even if this isnโ€™t a direct implementation:
Discussions

reflection - Java - Check if an object (class) implements an interface - Stack Overflow
I'm trying to check if an object implements an interface (If that class of that object implements the interface) in Java. More on stackoverflow.com
๐ŸŒ stackoverflow.com
May 16, 2013
java - How to check if an object implements an interface? - Stack Overflow
How to check if some class implements interface? More on stackoverflow.com
๐ŸŒ stackoverflow.com
class - How to check if object implements particular interface, but not the descendants of this interface, in Java - Stack Overflow
I have a tree structure, where some nodes must contain only objects implementing particular interface. But there is interfaces extending that interface, and objects, implementing them, should not be More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - Is it bad programming practice to check if a class referenced by its interface is an instance of another class? - Software Engineering Stack Exchange
That seems a little odd as well, since most (2 out of 3) of the objects that implement the interface don't have a target. ... That is the OO way. Only thing to check is if the hasTarget() method actually makes sense in a Timable. More on softwareengineering.stackexchange.com
๐ŸŒ softwareengineering.stackexchange.com
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 529352 โ€บ java โ€บ Check-Class-implements-interface
Check if Class implements interface (Java in General forum at Coderanch)
I do want to check before I create the object, but this method seems to be the wrong way round. isAssignableFrom public boolean isAssignableFrom(Class<?> cls) 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.
๐ŸŒ
Quora
quora.com โ€บ How-do-you-know-if-an-object-implements-an-interface-in-Java
How to know if an object implements an interface in Java - Quora
Answer: In theory, as the other ... see which interfaces it implements. A list of interfaces will appear following the keyword [code ]implements[/code], which follows the name of the class....
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ java-how-to-check-if-an-object-implements-an-interface-in-java-560009
How to Check If an Object Implements an Interface in Java | LabEx
In this lab, we learned how to check if an object implements a specific interface in Java using the instanceof keyword. We started by defining a simple interface (Printable) and a class (Document) that implements it.
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-check-if-an-object-represents-an-interface-in-java
How to check if an object represents an interface in Java
Otherwise, the method returns false, indicating that the given class is not an interface. ... Line 4: We define a string object stringObject. Line 5: We use the isInterface() method to check if the stringObject belongs to an interface.
๐ŸŒ
Quora
quora.com โ€บ How-do-you-find-out-if-a-class-implements-an-interface-in-Java-How-do-you-check-it-programmatically
How to find out if a class implements an interface in Java? How do you check it programmatically - Quora
Answer (1 of 2): If you have the source code, you can see the implements clause. Otherwise, check the Javadoc for the class. It will list the interfaces along with the superclass.
Find elsewhere
๐ŸŒ
IIT Kanpur
iitk.ac.in โ€บ esc101 โ€บ 05Aug โ€บ tutorial โ€บ reflect โ€บ class โ€บ getInterfaces.html
Identifying the Interfaces Implemented by a Class
You can invoke the getName method on the Class objects in the array returned by getInterfaces to retrieve the interface names. To find out how to get additional information about interfaces, see the section Examining Interfaces. The program that follows prints the interfaces implemented by ...
๐ŸŒ
CodingTechRoom
codingtechroom.com โ€บ question โ€บ check-if-class-implements-interface-java
How to Determine If a Class Implements an Interface in Java? - CodingTechRoom
This approach is straightforward and effective for verifying interface implementation. ... // Check if the object 'gor' is an instance of the 'Monster' interface if (gor instanceof Monster) { System.out.println("Gorgon implements Monster interface."); } else { System.out.println("Gorgon does not implement Monster interface."); }
๐ŸŒ
Alvin Alexander
alvinalexander.com โ€บ java โ€บ java-instanceof-interface-example
Java instanceof interface example | alvinalexander.com
*/ public class JavaInstanceofInterfaceExample2 { public static void main(String[] args) { Dog dog = new Dog(); // instanceof object test if (dog instanceof Dog) System.out.println("dog is an instanceof Dog"); // instanceof interface test if (dog instanceof Animal) System.out.println("dog is an instanceof Animal"); // instanceof interface test #2 if (dog instanceof LifeForm) System.out.println("dog is an instanceof LifeForm"); } } interface LifeForm {} interface Animal extends LifeForm {} class Dog implements Animal {} Here are links to some of our other Java instanceof tutorials:
๐ŸŒ
Electro4u
electro4u.net โ€บ blog โ€บ determine-if-a-class-implements-an-interface-in-java--1308
Determine if a Class Implements an Interface in Java
The easiest way to determine if a class implements an interface is to use the instanceof operator. This operator takes an object, and a type. It returns true if the object is an instance of the specified type, or false otherwise.
๐ŸŒ
javathinking
javathinking.com โ€บ blog โ€บ determine-if-a-class-implements-a-interface-in-java
How to Determine if a Java Class Implements an Interface Using Class Object โ€” javathinking.com
To check if MyClass implements MyInterface using getInterfaces(): public class InterfaceChecker { public static void main(String[] args) { Class<?> myClass = MyClass.class; Class<?>[] interfaces = myClass.getInterfaces(); boolean implements...
Top answer
1 of 4
10

Yes this is indeed a bad practice. You are using an interface to abstract your code from the implementation. When you work with the interface and access the implementation (via casting) you are violating the interface definition.

To solve this kind of problem extend your interface by another method, that you would access from within your code.

public Timable[] findObjectsWithTarget(Cue target) {
    ArrayList<Timable> result = new ArrayList<Timable>();
    for (Timable timed : fireable) { //fireable is the array (actually a HashSet) of Timed objects
        if (timed.hasTarget(target))
            result.add(timed);
    }
    return (Timable[]) result.toArray();
}

You have to implement that method also in Timerclass but you can return false per default.

2 of 4
9

Yes, it's a bad practice, because you're adding functionality to the Timeable interface that you're not specifically declaring, namely the possibility of also being a Trigger. There are a few different ways to do it better:

  • Explicitly add the functionality to the Timeable interface, as in woni's answer. This is better than using instanceof, but still violates the interface segregation principle.
  • Keep separate lists of Timeable and Trigger objects in the calling code, or at the very least, only keep a combined list in contexts where the Trigger functionality isn't necessary.
  • Keep a back reference from your target into all the Timeable objects that use it. In other words, call target.getTriggers() instead of timeableList.findObjectsWithTarget(target). Often when a method feels awkward, it's because you've put it into the wrong class. The fact that "a lot of methods" need to do this search means this approach would also have significant time efficiency benefits.
๐ŸŒ
CodingTechRoom
codingtechroom.com โ€บ question โ€บ how-to-check-if-class-implements-interface-java
How to Check If a Class Implements a Specific Interface in Java - CodingTechRoom
To determine if a Class object implements a particular interface, we can utilize the `isAssignableFrom()` method of the Class class. ... Class[] classes = ClassUtils.getClasses(handlersPackage); for (Class clazz : classes) { if (SomeInterface.class.isAssignableFrom(clazz)) { retVal.put(cla...
๐ŸŒ
CodingTechRoom
codingtechroom.com โ€บ question โ€บ how-to-check-if-an-object-implements-an-interface-in-java
How to Check if an Object Implements a Specific Interface in Java? - CodingTechRoom
public static Boolean implementsInterface(Object object, Class<?> interf) { for (Class<?> c : object.getClass().getInterfaces()) { if (c.equals(interf)) { return true; } } return false; } In Java, determining whether an object implements a specific interface can be crucial for ensuring type safety and flexibility in code.
๐ŸŒ
Iditect
iditect.com โ€บ faq โ€บ java โ€บ determine-if-a-class-implements-a-interface-in-java.html
Determine if a Class implements a interface in Java
Replace myObject with the object you want to check and MyInterface with the interface you want to check for. You can use reflection to check if a class implements a specific interface. This method allows you to check the class type itself and is suitable for compile-time type checking: import ...