How can I know all the classes that are extended to another abstract class?

4

Hello everyone JAVA

Well, these last days I was working on a project of a game and some asked me to implement a kind of "API" so that they can make their own "PLAYERS", example:

public class ejemplo extends MakeAPlayer{}

"MakeAPlayer" contains abstract methods that they must fill (@Override), but I can never register their class in my plugin, is there any way ?, Could it work with a HashSet in a constructor ?, < em> Thanks to the respondent!

    
asked by Java doub 20.08.2016 в 06:04
source

1 answer

1

If I have not misunderstood, you should ask the objects a question, your code would look like this:

class Animal {}
class Perro extends Animal {
   public static void main (String[] args){
     Perro toby = new Perro();
     if (toby instanceof Animal)
       System.out.println("toby es un perro y también un animal");
   }
}

In this way with the instanceof you know if they extend from one class or another, where I have Animal you will put the class that interests you as MakeAPlayer .

I hope I have helped you, but you already know, tell me about it.

I continue with your comment:

So what you need is a creation of a generic class where this will be a subtype of your class MakeAPlayer .

An example code could be like this:

 Person p = new Person();   

Class<? extends Person> clase = p.getClass();  

try {  
    Person p =  clase.newInstance();  
} catch (InstantiationException e) {  
    System.out.println(p.getName() + " no puede ser instanciada");  
    System.out.println("Puede ser que " + p.getName() + " sea una clase abstracta," +  
        " una interfaz, un vector (array), a primitive type, or void;");          
    e.printStackTrace();  
} catch (IllegalAccessException e) {  
    System.out.println("La clase " + p.getName() + "no tiene un constructor " +   
        "vacío y público");  
        e.printStackTrace();  
} // try  

I leave you a source of information where you explain in a synthesized way what you need. link

answered by 20.08.2016 в 14:07