Pin It

How to determine Interfaces implemented by a class dynamically using Java Reflection API ?.

Program to demonstrate how to determine Interfaces implemented by a class dynamically using Java Reflection API.

package com.hubberspot.reflection;


// Created two three interfaces just for the demo
// such as Shape, Rotation and Movable
interface Shape
{

}

interface Rotation 
{

}

interface Movable
{

}

// Created a class which implements all those interfaces
class Circle implements Shape , Rotation , Movable 
{

}


public class InterfacesFinding {

 public static void main(String[] args) {

  // Get the instance of Class of the type Circle 
  Class classCircle = Circle.class;

  // class Class defines a method which returns
  // an array of interfaces 
  Class[] interfacesArray = classCircle.getInterfaces();

  System.out.println("Interfaces implemented by Circle class are : ");

  for (int i = 0; i < interfacesArray.length; i++) {

   String interfaceName = interfacesArray[i].getName();

   System.out.println(i + 1 + " " +interfaceName);
  }

 }

}


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