- Java instanceof Operator
- Example: Java instanceof
- Java instanceof during Inheritance
- Java instanceof in Interface
- java check if class implements interface
- java check if class implements interface
- java check if class implements interface
- Check if Class implements interface
- How do you check if a Java class implements an interface?
- How do you check if a Java class implements an interface?
- How do you check if an object is an instance of an interface Java?
- How do you know if a class is an interface?
- Can you use Instanceof on an interface?
- Can we declare constructor inside an interface?
- How to implement interface with a class in Java?
- How are interfaces used in inheritance in Java?
- How are interfaces used for abstraction in Java?
Java instanceof Operator
The instanceof operator in Java is used to check whether an object is an instance of a particular class or not.
objectName instanceOf className;
Here, if objectName is an instance of className , the operator returns true . Otherwise, it returns false .
Example: Java instanceof
name is an instance of String: true obj is an instance of Main: true
In the above example, we have created a variable name of the String type and an object obj of the Main class.
Here, we have used the instanceof operator to check whether name and obj are instances of the String and Main class respectively. And, the operator returns true in both cases.
Note: In Java, String is a class rather than a primitive data type. To learn more, visit Java String.
Java instanceof during Inheritance
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 Main < public static void main(String[] args) < // create an object of the subclass Dog d1 = new Dog(); // checks if d1 is an instance of the subclass System.out.println(d1 instanceof Dog); // prints true // checks if d1 is an instance of the superclass System.out.println(d1 instanceof Animal); // prints true >>
In the above example, we have created a subclass Dog that inherits from the superclass Animal . We have created an object d1 of the Dog class.
Inside the print statement, notice the expression,
Here, we are using the instanceof operator to check whether d1 is also an instance of the superclass Animal .
Java instanceof in Interface
The instanceof operator is also used to check whether an object of a class is also an instance of the interface implemented by the class. For example,
// Java program to check if an object of a class is also // an instance of the interface implemented by the class interface Animal < >class Dog implements Animal < >class Main < public static void main(String[] args) < // create an object of the Dog class Dog d1 = new Dog(); // checks if the object of Dog // is also an instance of Animal System.out.println(d1 instanceof Animal); // returns true >>
In the above example, the Dog class implements the Animal interface. Inside the print statement, notice the expression,
Here, d1 is an instance of Dog class. The instanceof operator checks if d1 is also an instance of the interface Animal .
Note: In Java, all the classes are inherited from the Object class. So, instances of all the classes are also an instance of the Object class.
In the previous example, if we check,
java check if class implements interface
ParaEagle
1 2 3if (SomeInterface.class.isAssignableFrom(SomeClass.class)) // . >
java check if class implements interface
Kara
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21// Example interface interface SomeInterface // . > // Example class class SomeClass implements SomeInterface // . > // Inheritance testing: Class interfaceType = SomeInterface.class; Class classType = SomeClass.class; if (interfaceType.isAssignableFrom(classType)) // . >
java check if class implements interface
ParaEagle
1 2 3 4 5 6Class interfaceType = SomeInterface.class; Class classType = SomeClass.class; if (interfaceType.isAssignableFrom(classType)) // . >
Our content is created by volunteers — like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Check if Class implements interface
posted 12 years ago
I’m writing a GUI to launch the classes in a package that implement Runnable.
I have an array of Class objects, and some of the classes represented implement the Runnable interface.
1) How do I check to see if the Class in question implements Runnable?
2) If it does, how do I instantiate and run an object of that class?
Of course, the Class objects in the ArrayList don’t themselves implement Runnable, but I want to find out about the classes they represent.
There is an isInstance method that can be used on a Class, but I don’t think this is what I need since it takes an object as an argument and I don’t see how this applies here.
I found someone with a similar problem here, but reading that thread I’m not much more enlightened (is the conclusion that you have to use BCEL?).
Saloon Keeper
posted 12 years ago
posted 12 years ago
I’ve worked out how to run it, but not how to check if it’s Runnable
Saloon Keeper
posted 12 years ago
posted 12 years ago
Stephan van Hulst wrote: Why do you have a list of Class? Why not just a list of Runnable?
It’s not a list of Runnables because I use a method (borrowed from here) to get the list of classes in the package.
I can’t be sure that they all implement Runnable, so I want to check if they do. I can just create an instance and try casting it as above and catch an exception, but would be cleaner to check if it represents a Runnable first.
Bartender
posted 12 years ago
- 1
As Stephan mentions, there’s the instanceof operator. You could use that to check the object you’ve created after you’ve created it (and before you’ve cast it to Runnable, obviously).
Or, if you want to check before you create the object, have a look at the isAssignableFrom method of Class.
Saloon Keeper
posted 12 years ago
- 1
I’m curious though, why would you want to do this? What is the purpose of your code?
Keep in mind that your code is very dangerous. Anyone could just drop a class file in that package and your code would run it and it would potentially wreak havoc.
Bartender
posted 12 years ago
One other thing to consider — is there any chance that any of your classes don’t have a no-arg constructor? If so you’ll get an exception when you use Class.newInstance(), so you’ll have to cope with that possibility.
[Edit: and what Stephan said ]
posted 12 years ago
Stephan van Hulst wrote: You know about the instanceof operator, right?
I was thinking of testing before I instantiate the object, but I can’t see how to do that.
Saloon Keeper
posted 12 years ago
Luigi Plinge wrote: I was thinking of testing before I instantiate the object, but I can’t see how to do that.
posted 12 years ago
Stephan van Hulst wrote: I’m curious though, why would you want to do this? What is the purpose of your code?
Keep in mind that your code is very dangerous. Anyone could just drop a class file in that package and your code would run it and it would potentially wreak havoc.
I’m just doing a bunch of the Project Euler exercises and each exercise solution has its own class. Just for my own benefit really, I just want to be able to kick off any solution from the project’s Main class, automatically benchmarking the running time. Instead of having a ton of different public static void main(String[] args) methods, I just implement Runnable and kick the solution off in a run() method.
posted 12 years ago
[Edit: and what Stephan said ]Matthew Brown wrote: One other thing to consider — is there any chance that any of your classes don’t have a no-arg constructor? If so you’ll get an exception when you use Class.newInstance(), so you’ll have to cope with that possibility.
Good point — well so far there is no chance, but I’ll have to be careful how I write the constructor if there is one.
Which has got me thinking. what if I do want to pass in some arguments? I guess I could run a setParameters(. ) method on the instance before running it.
posted 12 years ago
Matthew Brown wrote:
Or, if you want to check before you create the object, have a look at the isAssignableFrom method of Class.
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. It returns true if so; otherwise it returns false. If this Class object represents a primitive type, this method returns true if the specified Class parameter is exactly this Class object; otherwise it returns false.
Specifically, this method tests whether the type represented by the specified Class parameter can be converted to the type represented by this Class object via an identity conversion or via a widening reference conversion. See The Java Language Specification, sections 5.1.1 and 5.1.4 , for details.
It seems like I would need to run this method on a Runnable object and pass my Class in as an argument. I can’t see how to do this.
How do you check if a Java class implements an interface?
How do you check if a Java class implements an interface?
You can use the isInterface() method call of the java. lang. Class to identify if a class objects represent an interface type.
How do you check if an object is an instance of an interface Java?
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. It returns either true or false.
Does class implement interface Java?
A class implements an interface, thereby inheriting the abstract methods of the interface. Along with abstract methods, an interface may also contain constants, default methods, static methods, and nested types. Method bodies exist only for default methods and static methods.
How do you know if a class is an interface?
You can use class. getInterfaces() and then check to see if the interface class is in there.
Can you use Instanceof on an interface?
instanceof can be used to test if an object is a direct or descended instance of a given class. instanceof can also be used with interfaces even though interfaces can’t be instantiated like classes.
Can we create instance of interface in Java?
We can’t create instance(interface can’t be instantiated) of interface but we can make reference of it that refers to the Object of its implementing class. A class can implement more than one interface. A class that implements interface must implements all the methods in interface.
Can we declare constructor inside an interface?
No, you cannot have a constructor within an interface in Java. You can have only public, static, final variables and, public, abstract, methods as of Java7.
How to implement interface with a class in Java?
How to test if object implements interface stack overflow?