Pin It

Using Reflection API to determine whether class object is an Interface or not

Program to demonstrate how Reflection API can be used to determine whether a class object is an Interface or not in Java

package com.hubberspot.reflection;

interface Shape {
 
}

class Rectangle implements Shape {
 
}

public class InterfaceInfo {
 
   public static void main(String[] args) {
   // Create a Rectangle object 
 Rectangle rectangle = new Rectangle();
    
 Class shapeInterface = Shape.class;  
 Class rectangleClass = rectangle.getClass();
  
 // isInterface() checks whether type is an interface or not
 if(shapeInterface.isInterface()) {
 // prints the simple name of the class
           System.out.println("Given type " + shapeInterface.getSimpleName()
    + " is an Interface");
 } else {
    System.out.println("Given class " + shapeInterface.getSimpleName()
    + " is not an Interface");
 }

 if(rectangleClass.isInterface()) {
    System.out.println("Given class " + rectangleClass.getSimpleName()
    + " is an Interface");
 } else {
    System.out.println("Given class " + rectangleClass.getSimpleName()
    + " is not an Interface");
 }
  

    }

}




Output of the program :





Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot