Monday, March 5, 2012

How do I check if a class represent an interface type in Java?



public class IsInterfaceDemo {
    public static void main(String[] args) {
        IsInterfaceDemo.get(Serializable.class);
        IsInterfaceDemo.get(Long.class);
    }

    private static void get(Class clazz) {
        if (clazz.isInterface()) {
            System.out.println(clazz.getName() +
                    " is an interface type.");
        } else {
            System.out.println(clazz.getName() +
                    " is not an interface type.");
        }
    }
}
Below is the output...

java.io.Serializable is an interface type.
java.lang.Long is not an interface type.

No comments:

Post a Comment