• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to add all sub classes of a specific class to an array and then print them to the console?

 
Greenhorn
Posts: 8
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to add all sub-classes of a specific class to an array and then print them to the console? I am working on a Java project and want to be able to get all of the sub-classes of the class "Throwable". I am new to Java. How do I do that?
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're new to Java then I don't know why you would want to do that. In general it isn't possible unless there's some kind of advanced hackery which I'm not familiar with, which is possible but if it exists then it won't be beginner-friendly. Could you explain your requirement and where it came from?
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

To elaborate on what Paul said: in object‑oriented programming, a superclass is not “aware” of what subclasses it has. You can try navigating a file system, but you will only find the classes you created yourself. You can try examining the API documentation and looking at known subclasses, but that has to be done recursively. That will only find you classes in the standard API or in a particular application. Otherwise, as Paul said, it is a task neither desirable nor readily feasible.
You can try the grep tool or similar and look for, “extends Throwable,” but again that has to be done recursively, and will fail if you have a class called Throwable, Exception, or similar.
 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I read the question differently. Given a super-class and some of its sub-classes, how would you make an array and populate it with each of the sub-classes, and then print them out from the array. Essentially: demonstrate polymorphism.
 
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shrest,

the easiest way i know to do what you ask is by making the Array, i like to use an array list, of type of the root class.  No matter how many times you extend a class, it still remains a root class object.  what you should also do though, is to make a routine in your classes that knows how to print the class to the console according to your needs.

i had a similar problem with graphic based object for a game i was working on: i used an interface to keep the needed methods and properties common, but a diverse population of graphic objects were added to a collection--an arraylist--for the graphics engine to process.  it worked very well from my needs.

i don't see why that type of approach would not work well for you too.

Les
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:I read the question differently. Given a super-class and some of its sub-classes, how would you make an array and populate it with each of the sub-classes...



But your code populates the array with an instance of each of the sub-classes, not each of the sub-classes. However, yeah, I can see that a beginner could easily miss making that distinction. You could be right.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would add a bit more polymorphism:I'm not sure I like the name Parent for a superclass; I think the best names are what they use on C#: base class and derived class
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What has this got to do with Throwable as in the first post?
 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I subclass Throwable (and Exception, which is Throwable) all the time. So you can never have a definitive universal list.

For a given Java app, you can certainly search its classpaths and introspect each class. Then again, JavaDoc already knows how to do that as long as you have all the source code.
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:What has this got to do with Throwable as in the first post?



The "Throwable" thing also confused me. I mean, it only has two subclasses (in the official Java API) so it would be easy enough to write Carey's code with Throwable and its two (official and publicly visible) subclasses. But it just seemed pretty far from being a normal thing to use to teach beginners.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got the feeling that "Throwable" was just something that the OP injected into the assignment because it was the first example of inheritance that came to mind.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The OP seems to have left the building. Shrest, are you still with us?
 
Shrest Burra
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:I got the feeling that "Throwable" was just something that the OP injected into the assignment because it was the first example of inheritance that came to mind.



Yes, I meant Exception instead of Throwable. Sorry for the inconvenience and misunderstanding
 
Shrest Burra
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:The OP seems to have left the building. Shrest, are you still with us?



I am still here
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shrest Burra wrote:

Carey Brown wrote:I got the feeling that "Throwable" was just something that the OP injected into the assignment because it was the first example of inheritance that came to mind.


Yes, I meant Exception instead of Throwable. Sorry for the inconvenience and misunderstanding

Why did you pick "Exception"? Was that actually part of the requirements? Could have chosen something else? If you use Exception do you plan on finding all sub-classes, or just demonstrate with just a couple?
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please don't edit posts after they have had replies; that makes the thread confusing. I am refusing the changes.
 
Shrest Burra
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

Shrest Burra wrote:

Carey Brown wrote:I got the feeling that "Throwable" was just something that the OP injected into the assignment because it was the first example of inheritance that came to mind.


Yes, I meant Exception instead of Throwable. Sorry for the inconvenience and misunderstanding

Why did you pick "Exception"? Was that actually part of the requirements? Could have chosen something else? If you use Exception do you plan on finding all sub-classes, or just demonstrate with just a couple?



I just wanted all classes that extend "Exception" - I want ones I defined and ones that were pre-defined in the JRE.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shrest Burra wrote:. . . classes that extend "Exception" - I want ones I defined and ones that were pre-defined in the JRE.

Look at the documentation and the TREE heading at the top of the page. Search for java.lang.Exception and scroll down until the bullets move back to the left. There are 466 classes shown there.
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, there's less than 100 classes in "Direct Known Subclasses" under java.lang.Exception in the documentation. It shouldn't be too hard to copy them and paste them into Carey's skeleton code.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The TREE view includes subclasses, subsubclasses, subsubsubclasses, etc. I did include Exception because if you ever write Foo<X extends Exception> then every class is a subtype of itself. Otherwise you only have 465.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic