• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance Tree for any class in Java

 
Anda Cristea
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I think that I want to display subclasses of any class like a tree.
The format of java classes in which is displayed in documentation is not usable.
Did you know any tool to help me?

Or any idea how to develop code to display inheritance tree for any classes?

Thank you in advance
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anda Cristea wrote:Did you know any tool to help me?

Probably any free UML diagram drawing tool will do the job. You could also use some drawing tools like paint and/or paint.net. But maybe it is much easier and faster to just use (plain old) pen and paper.
 
Anda Cristea
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Anda Cristea wrote:Did you know any tool to help me?

Probably any free UML diagram drawing tool will do the job. You could also use some drawing tools like paint and/or paint.net. But maybe it is much easier and faster to just use (plain old) pen and paper.


I want to generate automatically. To have input for execplu: JavaIOException and to see their subclasses.

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anda Cristea wrote:I want to generate automatically. To have input for execplu: JavaIOException and to see their subclasses.

I see.

To generate this automatically you need to use the getSuperclass() method of the Class class to list all superclasses. If you are interested in all implemented interfaces, you need the getInterfaces() method of the same class.

And this exercise is a nice example to get some coding practice. And if you implement it correctly, this command will result in the following output:
java.io.FileNotFoundException
java.io.IOException
java.lang.Exception
java.lang.Throwable
java.lang.Object


Hope it helps!
Kind regards,
Roel
 
Anda Cristea
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:And this exercise is a nice example to get some coding practice.

Yes, great solution.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And have you already written this little utility class?
 
Anda Cristea
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:And have you already written this little utility class?


Hi,

I didn't find the method who returrn all subclasses.
For suplerclass I fiind the method.



 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anda Cristea wrote:I didn't find the method who returrn all subclasses.

Such a method does not exist. It would be a very slow method as it should scan the complete class path to look for any subclass. And can you imagine what would happen if you invoke this method on the java.lang.Object class?
 
Anda Cristea
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Anda Cristea wrote:I didn't find the method who returrn all subclasses.

Such a method does not exist. It would be a very slow method as it should scan the complete class path to look for any subclass. And can you imagine what would happen if you invoke this method on the java.lang.Object class?


Yes, I imagine.
The Tree that would be result to had many many nodes.
:-)
 
Anda Cristea
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anda Cristea wrote:
Roel De Nijs wrote:
Anda Cristea wrote:I didn't find the method who returrn all subclasses.

Such a method does not exist. It would be a very slow method as it should scan the complete class path to look for any subclass. And can you imagine what would happen if you invoke this method on the java.lang.Object class?


Yes, I imagine.
The Tree that would be result to had many many nodes.
:-)


Here is my code:

     
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anda Cristea wrote:Here is my code:

What if a class has (indirectly) more than 1 superclass?
 
Anda Cristea
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Anda Cristea wrote:Here is my code:

What if a class has (indirectly) more than 1 superclass?


If a class has many superclases I will use an array.

I think  that a solution would exist.
That solution is used in java documentation when for a class has displayed known subclasses.

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anda Cristea wrote:If a class has many superclases I will use an array.

Do you think an array is the best choice?

Anda Cristea wrote:I think  that a solution would exist.
That solution is used in java documentation when for a class has displayed known subclasses.

But the java documentation is generated using the javadoc tool. The only thing you can do, is going through all the classes on your class path and see which ones inherit from the given class.
 
Paul Clapham
Sheriff
Posts: 21583
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:But the java documentation is generated using the javadoc tool.


It's worth pointing out that the javadoc tool uses source code as its input. And that implies that the javadoc tool knows how to parse Java code and extract class and interface relationships.
 
Paul Clapham
Sheriff
Posts: 21583
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also I don't understand how this question is relevant to the OJACP exam so I'm moving it to Java in General.
 
Campbell Ritchie
Sheriff
Posts: 51456
87
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look through the API docuemntation for System, which is a utility class and therefore has a private constructor only. Then try the same for String, which is immutable and is marked final. So far, so good. Now look at IOException, which has subclasses, as we already know. Notice the difference? Just above the horizontal rule separating the heading from the description, there is a section marked
Direct Known Subclasses:
Look at that. Is that what you are looking for? That is doubtless created by the javadoc tool. I have never tried that with my own classes, but you can try it out.

You cannot expect classes created by somebody else on a different machine to appear there. Should I create...then there is no way the Oracle documentation tool will ever know about it.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:It's worth pointing out that the javadoc tool uses source code as its input. And that implies that the javadoc tool knows how to parse Java code and extract class and interface relationships.

Of course, it does! And it even has the ability to exclude classes, otherwise there would be a gazillion classes listed under java.lang.Object. And in any IDE you can probably request the type hierarchy of a class as well. Bottom line: there is no method in the Java API to get all subclasses of a given class (like the getSuperclass() method of the Class class to get the superclass of a class), you'll have to iterate through all the classes on the class path and do some analysis to get all subclasses (which will be a pretty slow process).

Paul Clapham wrote:Also I don't understand how this question is relevant to the OJACP exam so I'm moving it to Java in General.

This user is clearly preparing for the OCAJP certification exam (based on his prior topics/posts), and he probably wants to use this program/tool during his further preparation.
 
Campbell Ritchie
Sheriff
Posts: 51456
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I could have sworn I posted something about this yesterday. Let's see if I can't find the .java files. ... Got them:-Run javadoc *.java and open the index.html file. From that navigate to the Super class documentation and then look under known subclasses. See if you can guess what will happen in advance.
 
Anda Cristea
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:. . . This user is clearly preparing for the OCAJP certification exam (based on his prior topics/posts), and he probably wants to use this program/tool during his further preparation.


Yes, the below afirmation is correct.
 
Anda Cristea
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Have a look through the API docuemntation for System, which is a utility class and therefore has a private constructor only. Then try the same for String, which is immutable and is marked final. So far, so good. Now look at IOException, which has subclasses, as we already know. Notice the difference? Just above the horizontal rule separating the heading from the description, there is a section marked
Direct Known Subclasses:
Look at that. Is that what you are looking for? That is doubtless created by the javadoc tool. I have never tried that with my own classes, but you can try it out.

You cannot expect classes created by somebody else on a different machine to appear there. Should I create...then there is no way the Oracle documentation tool will ever know about it.


I want a way to learn easily the subclasses of exception classes.
The way in which Oracle present subclasses is not friendly.
 
Rob Spoor
Sheriff
Pie
Posts: 20753
68
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At the top of the Javadoc pages, there's a link called Tree. If you open it, it shows the inheritance tree of the JSE code. If you click on "Tree" while you've opened a specific package, it shows the tree for only that package (and the super classes for the classes inside). If you now search for Throwable, you see the root of the exception class hierarchy.

You can only see the JSE classes in there though, because the JSE documentation simply doesn't know any other classes. Other APIs have similar inheritance trees. The JSE super classes used are still listed though. For instance, in the Apache Commons Lang inheritance tree you can also search for Throwable to find their specific exceptions.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic