Examining Classes |
Class
objects represent interfaces as well as classes. If you aren't sure whether aClass
object represents an interface or a class, call theisInterface
method.You invoke
Class
methods to get information about an interface. To find the public constants of an interface, invoke thegetFields
method upon theClass
object that represents the interface. The section Identifying Class Fields has an example containinggetFields
. You can usegetMethods
to get information about an interface's methods. See the section Obtaining Method Information. To find out about an interface's modifiers, invoke thegetModifiers
method. See the section Discovering Class Modifiers for an example.By calling
isInterface
, the following program reveals thatObserver
is an interface and thatObservable
is a class:The output of the preceding program is:import java.lang.reflect.*; import java.util.*; class SampleCheckInterface { public static void main(String[] args) { Class observer = Observer.class; Class observable = Observable.class; verifyInterface(observer); verifyInterface(observable); } static void verifyInterface(Class c) { String name = c.getName(); if (c.isInterface()) { System.out.println(name + " is an interface."); } else { System.out.println(name + " is a class."); } } }java.util.Observer is an interface. java.util.Observable is a class.
Examining Classes |