How do I get interfaces implemented by a class?

The getClass().getInterfaces() method return an array of Class that represents the interfaces implemented by an object.

package org.kodejava.example.lang;

import java.util.Date;
import java.util.Calendar;

public class ClassInterfaces {
    public static void main(String[] args) {
        // Get an instance of Date class
        Date date = Calendar.getInstance().getTime();

        // Get all interfaces implemented by the java.util.Date class and
        // print their names.
        Class[] interfaces = date.getClass().getInterfaces();
        for (Class iface : interfaces) {
            System.out.println("Interface Name = " + iface.getName());
        }

        // For the primitive type the interface will be an empty array.
        Class c = char.class;
        interfaces = c.getInterfaces();
        for (Class iface : interfaces) {
            System.out.println("Interface Name = " + iface.getName());
        }
    }
}

The java.util.Date class implements the following interfaces:

Interface Name = java.io.Serializable
Interface Name = java.lang.Cloneable
Interface Name = java.lang.Comparable

Wayan

Programmer, runner, recreational diver, live in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, Hibernate / JPA.

Leave a Reply