The getClass().getInterfaces()
method return an array of Class
that represents the interfaces implemented by an object.
package org.kodejava.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<?> i : interfaces) {
System.out.printf("Interface of %s = %s%n", date.getClass().getName(), i.getName());
}
// For the primitive type the interface will be an empty array.
interfaces = char.class.getInterfaces();
for (Class<?> i : interfaces) {
System.out.printf("Interface of %s = %s%n", char.class.getName(), i.getName());
}
}
}
The java.util.Date
class implements the following interfaces:
Interface of java.util.Date = java.io.Serializable
Interface of java.util.Date = java.lang.Cloneable
Interface of java.util.Date = java.lang.Comparable
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024