Determining the Interfaces Implemented by a Class


Class c = someObject.getClass(); Class[] interfaces = c.getInterfaces(); for (int i = 0; i < interfaces.length; i++) {    String interfaceName = interfaces[i].getName();    System.out.println(interfaceName); }



In the previous phrase, we show you how to find the superclasses associated with a given class. Superclasses are related to inheritance and the class extension mechanism in Java. In addition to extending a class, in Java you can implement an interface. Through reflection, you can also find which interfaces a given class has implemented. After you've obtained a Class object, you can use the getInterfaces() method to get the class's interfaces if indeed the class implements any interfaces. The getInterfaces() method will return an array of Class objects. Each object in the array represents one interface implemented by the given class. You can use the getName() method on these Class objects to get the name of the interfaces implemented.




JavaT Phrasebook. Essential Code and Commands
Java Phrasebook
ISBN: 0672329077
EAN: 2147483647
Year: 2004
Pages: 166

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net