How do I get interfaces implemented by a class?

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 intf : interfaces) {
            System.out.println("Interface Name = " + intf.getName());
        }

        //
        // For the primitive tipe the interface will be an empty array.
        //
        Class c = char.class;
        interfaces = c.getInterfaces();
        for (Class intf : interfaces) {
            System.out.println("Interface Name = " + intf.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 Saryada

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