mardi 5 mars 2019

java.lang.InstantiationException Erro when try instantiate a children class

Hi I have the following clases:

public abstract class A(){

    public void doSomething();

    public void makeMe(){
        try {
            A.class.getDeclaredConstructor().newInstance();
        } catch (InstantiationException | IllegalAccessException | IllegalArgumntException
                    | InvocationTargetException | NoSuchMethodException | SecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
        }
    }

}

public class B() extends A{
    @Override
    public void doSomething(){
    }    
}

public class C() extends A{
    @Override
    public void doSomething(){
    }    
}

How can do makeMe for that I can get the follow behavior:

B.makeMe()

C.makeMe()

The original class are more complex than this, this is only an example. But how can get it this, is it possible?

It is possible do this with reflections?

Thanks





Aucun commentaire:

Enregistrer un commentaire