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:
typeof(IMyInterface).IsAssignableFrom(typeof(MyType))
typeof(MyType).GetInterfaces().Contains(typeof(IMyInterface))
- 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
Quickest Way to Read First Line from File
Minimum Cart Amount for Specific Product Categories in Woocommerce
Extract Url's from a String Using PHP
Symfony2 Collection of Entities - How to Add/Remove Association with Existing Entities
How to Only Use Created_At in Laravel
HTML into PHP Variable (HTML Outside PHP Code)
How to Work with Many-To-Many Relations in Yii2
Easy Login Script Without Database
PHP Date Showing '1970-01-01 ' After Conversion
PHP - Count Frequency of Array Values
Sort Array by Length and Then Alphabetically
Learning Select from Where Prepared Statements
What Are the Differences Between "PHP Artisan Dump-Autoload" and "Composer Dump-Autoload"