Celeritas
Celeritas

Reputation: 15091

What to do when making interface where some implementations don't use a variable?

Say you have an interface Employee with the method signature int calculateWage(int hoursPerWeek, int overtime);. What if one implementation per week doesn't use the variable overtime? For example the code

int calculateWage(int hoursPerWeek, int overtime) {
  return hoursPerWeek*wage;
}

looks silly and produces warnings as overtime isn't used.

Upvotes: 0

Views: 88

Answers (3)

Roman Nikitchenko
Roman Nikitchenko

Reputation: 13046

Your interface (class?) really looks silly and suspicious for analyser. Obvious answer is to introduce 2 methods: 1 which doesn't have overtime parameter and one that has. Something like this:

int calculateWage(int hoursPerWeek, int overtime) {
    return (hoursPerWeek + overtim) * wage;
}

int calculateWage(int hoursPerWeek) {
    return calculateWage(hoursPerWeek, 0);
}

If you care about classes which will need to implement both methods - OK, in Java 8 you can base on default implementation and in practice good analyzer should distinguish this from other way you can follow:

interface WageCalculator {

    int calculateWage(int hoursPerWeek, int overtime);

}

class GreedyWageCalculator implements WageCalculator {

    @Override
    int calculateWage(int hoursPerWeek, int overtime) {
        return hoursPerWeek * wage;
    }

}

This approach is illustrated here.

And worst case you can rely on old dump @SuppressWarnings("unused") despite I personally dislike this approach. Another way is to use some 'empty' code fragment. There is couple of widely used one but again, I dislike this make your compiler happy practice. Question is slightly unclear: if we speak about interfaces, why implementation is here? If we speak about interfaces with default implementation you can always implement 2 methods like in my first example above.

Upvotes: 0

laune
laune

Reputation: 31300

If the interface assumes that hours-per-week includes the overtime hours, all implementations not treating overtime quantities with higher wage still should compute

 return (hoursPerWeek - overtime)*wage + overtime*wage;

with a comment that overtime isn't paid extra.

If the interface does not assume that hours-per-week include the overtime hours, the implmentation should either

 return hoursPerWeek*wage + overtime*0;  // why did they work overtime?

or

if( overtime > 0 ) throw new IllegalArgumentException( "..." ); // unexpected!
return hoursPerWeek*wage;

This will, in any case, uphold the contract made by the interface using defensive coding.

Upvotes: 1

ajb
ajb

Reputation: 31699

This is not a problem. Interfaces are used because they can, in theory, represent any number of different classes that implement those methods. It's entirely sensible that some of those classes (even if they exist only in theory) might use all of the parameters, and others might not.

In fact, I think it's a very normal occurrence for some of the interfaces defined by the language. In many, if not most, of the classes I've written that implement ActionListener, I don't use the parameter to ActionPerformed at all, because the particular implementation doesn't need it. But the information is there for a case where it is needed.

Upvotes: 1

Related Questions