mardi 7 novembre 2017

Is there a better way to create an abstract class that extends another abstract class and adds new features?

I have an abstract class that has a concrete method solveand several abstract methods that are used by solve:

public abstract class A{
    public void solve(){
       //code for solve

    }

    public abstract void a();
    public abstract void b();
}

Then I have another class B that extends A, reimplements the solve methods using the abstract methods from A and new ones added (c,d). It also has a boolean field that indicates if it has to use the solve A or the one from B.

public abstract class B extends A{

    public B(boolean useA){
        if(useA) super.solve();
        else // code for solve
    }
    public void solve(){

       //code for solve
    }

    public abstract void c();
    public abstract void d();
}

Then I have a concrete class C that extends B, implements all methods and has a boolean to indicate if solve has to be used or not.

public class C extends B{

    public C(boolean useA){
        super(useA);
    }

    ... //code for a,b,c and d
}

Is there any way to do this better? I think that maybe it's not following the principles of OOP.

Aucun commentaire:

Enregistrer un commentaire