mercredi 17 mai 2023

Best practice on adding a new method in a class implementing an interface with less parameters required

public interface DataAdapter {

    int solve(int a, int b, int c)
}

Now i have multiple classes which implements this interface something like :

public class DataAdapterImplA implements DataAdapter {

    int solve(int a, int b, int c) {

        return ...
    }
}

public class DataAdapterImplB implements DataAdapter {

    int solve(int a, int b, int c) {

        return ...
    }
}

and similarly multiple others ...

Now i am adding one more implementing class but with one less parameter in the existing method signature ..something like below :

public class DataAdapterImplF implements DataAdapter {

    int solve(int a, int b) {

        return ...
    }
}

So , question here is how to go about this :

  1. Should i override the existing solve(int, int, int) method only & in my method body i can ignore this and won't use it at all ?

  2. should i create a default method solve(int, int) in the interface & override that in my new implementation class ?

or any other better & clean solution ?

Aucun commentaire:

Enregistrer un commentaire