Program to demonstrate how Reflection API can be used to determine whether a class object is an Interface or not in Java
Output of the program :
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 :