Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

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 :





 
© 2021 Learn Java by Examples Template by Hubberspot