jeudi 1 décembre 2016

Call method of unknown subclass

I have abstract class in Java. I want to call to specific method of his subclass (if it exists) with reflection.

public abstract class Parent {
    public void doIt() {
        if (the caller class have method called "specialMethod") {
            call it
        }
    }
}

public class Child extends Parent{
    public void spacialMethod() {
        System.out.println("Child special method called");
    }
}

public class Child2 extends Parent{
    // Empty
}

So if I will run that code:

Child child = new Child();
child.doIt(); // Will print the text
Child2 child2 = new Child2();
child2.doIt(); // Nothing will happened

How can I check in the parent class if the current subclass have method called "specialMethod"?





Aucun commentaire:

Enregistrer un commentaire