jeudi 8 avril 2021

Abstract class Implementation

I have an abstract class as seen below. I have 2 concrete classes for this abstract class. My problem is that 1 of the concrete classes needs to implement a method that is not required by class2. So I wouldn't want to call that method in the abstract class as not all concretions depend on it. But the calling code will call the execute() method in the abstract class. It is the entry point to the program. My quesion is how would I refactor this

Abstract class A{

    abstract void doOne();
    abstract void doTwo();

    public void execute(){
        //.... Lots of common code used by both Class1 and Class2
        doOne();
        //....
        doTwo()
        //...
        //Method should only be implemented by Class 1 and not by Class 2
        CallOnlyClass1()
    }

}

Class class1 extends A{

}

Class class2 extends A{

Aucun commentaire:

Enregistrer un commentaire