[java] listing classes that inherit a particular class

Started by
10 comments, last by The_Neverending_Loop 12 years, 8 months ago
Hi,

I have an abstract class called gameResource. Several other classes extend this class, such as gameResourceTree and gameResourceGold.

I'd like to have one single function that would create a HashMap or something similar of all the classes that extend gameResource so that I can iterate through each class. All the sub-classes will exist in the same package, and therefore be loaded in the same JAR file.

I'd like not to have to maintain this list of sub-classes manually. Is this possible in Java?
Advertisement
Yes it is possible in Java. You can read up on Java Reflection to see how you can do it.
why do u want to write such ugly code?
can u describe to us what u want to achieve with this?

why do u want to write such ugly code?
can u describe to us what u want to achieve with this?


Putting aside the fact that you haven't actually seen my code, can you explain to me how my code is ugly?

How would you implement a world full of resources that players can interact with, if not by inheriting some common functionality from a base class?

Yes it is possible in Java. You can read up on Java Reflection to see how you can do it.


I did some reading about Java Reflection, but I'm not clear on how to do this still.

I looked at the getDeclaredClasses method, but that only appears to return classes that are defined inside a class, for example:

public class foo {
public class bar {
public int baz = 0;
}
}


In this case, if I called foo.class.getDeclaredClasses() I would get a list that included "bar". But, if I define these two classes:

public class foo {
}

public class bar extends foo {
}


In this case, foo.class.getDeclaredClasses() does not return "bar".

Or am I not understanding something?
why not have one single resource class, which has as attributes a resource-type(enum) and a value.

why not have one single resource class, which has as attributes a resource-type(enum) and a value.


Because each different type of resource behaves differently. They also all will have their own "draw" procedure; some will be done procedurally, and others drawn from a file created on Blender or whatever.
You have to give us more information about what u are doing. How do resources "behave" in anyway?

You should always favor composition over inheritance, so if you are having a lot of classes with the same base class there is probably something u can do about it.

You have to give us more information about what u are doing. How do resources "behave" in anyway?

You should always favor composition over inheritance, so if you are having a lot of classes with the same base class there is probably something u can do about it.


Well, for example:

When you harvest an apple tree, you get apples and the "draw" routine stops drawing the apples on the tree for a while thereafter.

When you harvest fish, you get fish, and maybe sometimes some other junk that got caught on your hook. Harvesting fish does not really change the visual representation of the resource, though.

When you harvest (let's say) exploding iron ore, you might get some iron or the iron node might explode, causing some health damage to you. In the case of exploding iron being successfully harvested, the resource would be drawn without iron ingots for a while. In the case where it exploded, it might draw a little crater for a while until the node re-spawns.

Some of the behaviors could be much more complex. For example, harvesting an ultra-rare resource much trigger a new quest for you, or it might cause several enemies to spawn right around the resource node to defend it. It might cause a warning to be sent to whichever faction controls the area to let them know that someone is pilfering their resources. It might cause you to be magically transported to a dungeon for some reason.

Id like to have one single function that would create a HashMap or something similar of all the classes that extend gameResource so that I can iterate through each class.
[/quote]
How are you going to use this HashMap? If you can show us how you plan to use this collection, we'd be in a better place to help you build it.

This topic is closed to new replies.

Advertisement