|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Because the Java programming language supports inheritance, an application such as a class browser must be able to identify superclasses. To determine the superclass of a class, you invoke thegetSuperclassmethod. This method returns aClassobject representing the superclass, or returns null if the class has no superclass. To identify all ancestors of a class, callgetSuperclassiteratively until it returns null.The program that follows finds the names of the
Buttonclass's ancestors by callinggetSuperclassiteratively.The output of the sample program verifies that the parent ofimport java.lang.reflect.*; import java.awt.*; class SampleSuper { public static void main(String[] args) { Button b = new Button(); printSuperclasses(b); } static void printSuperclasses(Object o) { Class subclass = o.getClass(); Class superclass = subclass.getSuperclass(); while (superclass != null) { String className = superclass.getName(); System.out.println(className); subclass = superclass; superclass = subclass.getSuperclass(); } } }ButtonisComponent, and that the parent ofComponentisObject:java.awt.Component java.lang.Object
|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.