I have a specific problem, which Eclipse solves perfectly, but I need a programmatic solution. What I want to do is get the "Type hierarchy" of any class that I provide. Eclipse does show a solution on pressing Ctrl+T, but how does it achieve this? Are there any APIs available so that I can use them?
The hierarchy of classes in Java has one root class, called Object , which is superclass of any class. Instance variable and methods are inherited down through the levels. In general, the further down in the hierarchy a class appears, the more specialized its behavior.
Steps to show class hierarchy Clicks on a class, press CTRL + T to view the subtype hierarchy. 2. Press CTRL + T again, it will display only the super type hierarchy.
In Java, the class hierarchy is tree like. In fact, not only is the hierarchy tree-like, Java provides a universal superclass called Object that is defined to be the root of the entire class hierarchy. Every class that is defined in a Java program implicitly extends the class Object.
A class hierarchy or inheritance tree in computer science is a classification of object types, denoting objects as the instantiations of classes (class is like a blueprint, the object is what is built from that blueprint) inter-relating the various classes by relationships such as "inherits", "extends", "is an ...
You can use Java's reflection API in order to get information about types at runtime.
For example, you can use Class.getSuperclass() to walk the type tree upwards, and find the parents of a class.
Java has a Reflection API which you can use to determine the base class of whatever class you have, as well as any interfaces that particular class implements. Determining what classes inherit from that class is going to be a bit more difficult, though. The reflection API also lets you do a lot of other stuff, too like determine what the members of that class are, and even call methods of that class and more.
public void DisplaySuperClass(Class c)
{
System.out.println(c.getSuperclass().getName());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With