The program that follows finds the names of the Button class's ancestors by calling getSuperclass iteratively.
import 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();
}
}
}
The output of the sample program verifies that the
parent of Button is Component, and that the parent
of Component is Object:
java.awt.Component
java.lang.Object