The JavaTM Tutorial

| Start of Tutorial > Start of Trail > Start of Lesson | Search

Trail: The Reflection API
Lesson: Examining Classes

## Examining Interfaces

> `Class` objects represent interfaces as well as classes. If you aren't sure whether a `Class` object represents an interface or a class, call the `isInterface` method.

> You invoke `Class` methods to get information about an interface. To find the public constants of an interface, invoke the `getFields` method upon the `Class` object that represents the interface. The section Identifying Class Fields has an example containing `getFields`. You can use `getMethods` to get information about an interface's methods. See the section Obtaining Method Information. To find out about an interface's modifiers, invoke the `getModifiers` method. See the section Discovering Class Modifiers for an example.

> By calling `isInterface`, the following program reveals that `Observer` is an interface and that `Observable` is a class:

> >     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.");
> >           }
> >        }
> >     }

> The output of the preceding program is:

> >     java.util.Observer is an interface.
> >     java.util.Observable is a class.

| Start of Tutorial > Start of Trail > Start of Lesson | Search