Possible Duplicate:
How do you find all subclasses of a given class in Java?
Hi,
I would like to get a list of classes that implement an interface in Java at runtime so I can do a lookup service without having to hard code it. Is there a simple way of doing this? I fear not.
A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass. A nested class has access to all the private members of its enclosing class—both fields and methods.
If you do have a string representing the name of a class and you want to find that class's subclasses, then there are two steps: find the class given its name, and then find the subclasses with __subclasses__ as above. However you find the class, cls. __subclasses__() would then return a list of its subclasses.
Keep in mind that you cannot choose multiple subclasses for the same class. You could, theoretically get 9 subclasses (assuming you meet the multiclassing requirements) with Cleric, Warlock, Sorcerer, Wizard, Druid, and any other 4.
The short answer is no.
The long answer is that subclasses can come into existence in many ways, which basically makes it impossible to categorically find them all.
You can't do it at runtime but you can't find classes until they're loaded and how do you know they're loaded? You could scan every JAR and class file but that's not definitive. Plus there are things like URL class loaders.
Inner classes (static and non-static) are another case to consider. Named inner classes are easier to find. Anonymous inner classes are potentially much more difficult to find.
You also have to consider that if a class has subclasses then new subclasses can be created at a later point.
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