Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the Class hierarchy in Java?

Tags:

java

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?

like image 394
Sonam Avatar asked Feb 02 '11 07:02

Sonam


People also ask

What is class hierarchy in Java?

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.

How do you show class hierarchy?

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.

What is hierarchy in Java with example?

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.

What is class hierarchy example?

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 ...


2 Answers

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.

like image 59
Avi Avatar answered Sep 24 '22 20:09

Avi


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());
}
like image 27
helloworld922 Avatar answered Sep 25 '22 20:09

helloworld922