Ads Sponsored By Google :)

Tuesday, 29 January 2013

how can we find|finding interfaces implemented|implements by class using reflection in java with example


Finding interfaces implemented by Class in java how can we find interfaces implemented by class in java
Finding interfaces implemented by Class in java with example Finding interfaces implemented by Class in java using reflection using reflection finding interfaces implemented by class  using reflection finding interfaces implemented by class with example can we find interfaces implmented by class in java using reflection
can we find interfaces implmented by class in java finding the interfaces implement by class in java
how can we find interfaces implemented by class in java with example Find if class implements interface
find the interfaces that are implmented by class in java how can we find interfaces implemented by class using reflection in java with example how can we find interfaces implemented by class using reflection in java



    Finding interfaces implemented by a class:

            In java one class can implement any number of interfaces so we say that java supports 
      multiple inheritance interface level. Once the class is loaded into JVM then we can get the 
     META DATA objects of its interfaces by calling getInterfaces().
      
           While loading a class into JVM,  a long with class its super class its super class its 
     implemented interfaces are also loaded into JVM automatically.

        JVM internally creates a separate META DATA object for super class and also for each 
   interface whenever we call getInterfaces method then JVM stores all META DATA objects 
  of interfaces into a class(array) then returns it.
  
      We need to get one by one class object from array using a loop then we need get name 
   of each interface by calling getName().

    For example:
     // ReflectDemo2.java

   public class ReflectDemo2{
    public static void main(String args[]){
            Class c=Class.forName(args[0]);
            Class it[]=c.getInterfaces();
             for(int i=0;i<it.length;i++) {
                   String s=it[i].getName();
                   System.out.println(“implemented interface “+s);
                 }
               }
             }

   Output:

                 C:\> javac ReflectDemo2.java
                 C:\> java ReflectDemo2 java.lang.Thread
                Implemented interface java.lang.Runnable


No comments:

Post a Comment

LinkWithin