java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to get interfaces implemented by a class?

package com.javacoderanch.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