|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
The type of an object is determined by not only its class and superclass, but also by its interfaces. In a class declaration, the interfaces are listed after theimplementskeyword. For example, theRandomAccessFileclass implements theDataOutputandDataInputinterfaces:You invoke thepublic class RandomAccessFile implements DataOutput, DataInputgetInterfacesmethod to determine which interfaces a class implements. ThegetInterfacesmethod returns an array ofClassobjects. The reflection API represents interfaces withClassobjects. EachClassobject in the array returned bygetInterfacesrepresents one of the interfaces implemented by the class. You can invoke thegetNamemethod on theClassobjects in the array returned bygetInterfacesto retrieve the interface names. To find out how to get additional information about interfaces, see the section Examining Interfaces.The program that follows prints the interfaces implemented by the
RandomAccessFileclass.Note that the interface names printed by the sample program are fully qualified:import java.lang.reflect.*; import java.io.*; class SampleInterface { public static void main(String[] args) { try { RandomAccessFile r = new RandomAccessFile("myfile", "r"); printInterfaceNames(r); } catch (IOException e) { System.out.println(e); } } static void printInterfaceNames(Object o) { Class c = o.getClass(); Class[] theInterfaces = c.getInterfaces(); for (int i = 0; i < theInterfaces.length; i++) { String interfaceName = theInterfaces[i].getName(); System.out.println(interfaceName); } } }java.io.DataOutput java.io.DataInput
|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.