Checking If an Instance's Class Implements an Interface

Checking if an instance's class implements an interface?

interface IInterface
{
}

class TheClass implements IInterface
{
}

$cls = new TheClass();
if ($cls instanceof IInterface) {
echo "yes";
}

You can use the "instanceof" operator. To use it, the left operand is a class instance and the right operand is an interface. It returns true if the object implements a particular interface.

How to check if a class implements a interface

You should use Class#isAssignableFrom(Class<?> cls) method here:

Class<?> classObj = MePOJO.class;
System.out.println(IPOJO.class.isAssignableFrom(classObj));

Output:

true

Determine if a Class implements a interface in Java

You should use isAssignableFrom:

if (YourInterface.class.isAssignableFrom(clazz)) {
...
}

Check if class implements interface given the class name

You can do that with Class.isAssignableFrom(Class):

if (MyInterface.class.isAssignableFrom(myClass)) {
...
}

How to check if an object implements an interface?

For an instance

Character.Gorgon gor = new Character.Gorgon();

Then do

gor instanceof Monster

For a Class instance do

Class<?> clazz = Character.Gorgon.class;
Monster.class.isAssignableFrom(clazz);

Check dynamically whether instance implements interface

Explanation

You need to pass the interface as Class (documentation) token. Also, you need to check the opposite way: interfaceToTestAgainst.isInstance(classToTest). Currently, you are trying to check whether the interface would be an instance of the class.

boolean doesImplementInterface(Object classToTest, Class<?> interfaceToTestAgainst) {
if (!interfaceToTestAgainst.isInterface()) {
return false;
}
return interfaceToTestAgainst.isInstance(classToTest);
}

or in one line:

boolean doesImplementInterface(Object classToTest, Class<?> interfaceToTestAgainst) {
return interfaceToTestAgainst.isInterface()
&& interfaceToTestAgainst.isInstance(classToTest);
}

Changed the naming a bit:

boolean isInstanceOfInterface(Object obj, Class<?> interfaceToken) {
return interfaceToken.isInterface()
&& interfaceToken.isInstance(obj);
}

A call of that method:

boolean result = isInstanceOfInterface(new Dog(), CanBark.class);

Note

Your question sounds like a XY problem. There might be way better solutions to solve what you are trying to solve with this attempt in the first place.

Losing type information, degrading the system to one which is not compile-time-safe anymore is generally very bad, if it can be avoided.

Consider re-thinking/-designing your approach. Just a note though, I do not know what you want to solve with that in the first place.

How to determine if a type implements an interface with C# reflection

You have a few choices:

  1. typeof(IMyInterface).IsAssignableFrom(typeof(MyType))
  2. typeof(MyType).GetInterfaces().Contains(typeof(IMyInterface))
  3. With C# 6 you can use typeof(MyType).GetInterface(nameof(IMyInterface)) != null

For a generic interface, it’s a bit different.

typeof(MyType).GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IMyInterface<>))


Related Topics



Leave a reply



Submit