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
Latest posts by Wayan (see all)
- How do I install Calibri font in Ubuntu? - January 24, 2021
- How do I create a generic class in Java? - January 1, 2021
- How do I convert java.util.TimeZone to java.time.ZoneId? - April 25, 2020