Saturday 30 September 2017

How to get the interfaces implemented by a class?

'java.lang.Class' provides 'getGenericInterfaces' and 'getInterfaces' methods to determine the interfaces implemented by given class.

getGenericInterfaces vs getInterfaces
getGenericInterfaces return the Types representing the interfaces directly implemented by the class or interface represented by this object, whereas getInterfaces return the interfaces implemented by the class or interface represented by this object.

Test.java
package com.sample.test;
import java.lang.reflect.Type;
import java.util.HashSet;

public class Test {
 public static void main(String args[]) {
  Class clazz = HashSet.class;

  System.out.println("Generic interfaces implemented by HashSet are : ");
  Type[] types = clazz.getGenericInterfaces();
  for (Type type : types) {
   System.out.println(type.getTypeName());
  }

  System.out.println("\n\nInterfaces implemented by HashSet are : ");
  types = clazz.getInterfaces();

  for (Type type : types) {
   System.out.println(type.getTypeName());
  }

 }
}


Output
Generic interfaces implemented by HashSet are : 
java.util.Set<E>
java.lang.Cloneable
java.io.Serializable


Interfaces implemented by HashSet are : 
java.util.Set
java.lang.Cloneable
java.io.Serializable


The order of the returned interfaces from methods getInterfaces, getGenericInterfaces is the order of the interface names in the implements clause of class definition.


TempSet.java
package com.sample.test;

public interface TempSet<T> {

}

IntegerSet.java
package com.sample.test;

import java.io.Serializable;

public class IntegerSet implements TempSet<Integer>, Cloneable, Serializable {

 private static final long serialVersionUID = 1L;

}


Test.java
package com.sample.test;

import java.lang.reflect.Type;

public class Test {
 public static void main(String args[]) {
  Class clazz = IntegerSet.class;

  System.out.println("Generic interfaces implemented by HashSet are : ");
  Type[] types = clazz.getGenericInterfaces();
  for (Type type : types) {
   System.out.println(type.getTypeName());
  }

  System.out.println("\n\nInterfaces implemented by HashSet are : ");
  types = clazz.getInterfaces();

  for (Type type : types) {
   System.out.println(type.getTypeName());
  }

 }
}


Output
Generic interfaces implemented by HashSet are : 
com.sample.test.TempSet<java.lang.Integer>
java.lang.Cloneable
java.io.Serializable


Interfaces implemented by HashSet are : 
com.sample.test.TempSet
java.lang.Cloneable
java.io.Serializable

As you notify the output, the interfaces returned are TempSet, Cloneable and Serializable, these are in the order they specified in IntegerSet implements clause.

Array types implements Cloneable and Serializable interfaces. You can check the same by calling getInterfaces and getGenericInterfaces methods.


Test.java
package com.sample.test;

import java.lang.reflect.Type;

public class Test {
 public static void main(String args[]) {
  int a[] = new int[10];
  Class clazz = a.getClass();

  System.out.println("Generic interfaces implemented by array are : ");
  Type[] types = clazz.getGenericInterfaces();
  for (Type type : types) {
   System.out.println(type.getTypeName());
  }

  System.out.println("\nInterfaces implemented by array are : ");
  types = clazz.getInterfaces();

  for (Type type : types) {
   System.out.println(type.getTypeName());
  }

 }
}

Output
Generic interfaces implemented by array are : 
java.lang.Cloneable
java.io.Serializable

Interfaces implemented by array are : 
java.lang.Cloneable
java.io.Serializable



No comments:

Post a Comment