I have an interface. For the sake of this question, I'll simplify it to this:
interface EraseableColoredPencil {
//This method seems appropriate for an interface;
//The implementation will change with each class that implements this interface.
void draw();
//This method does not seem as appropriate;
//The implementation of the erase method would be the same for all classes.
void erase();
}
My question is: According to OOP principles, what is the best way to express this? An interface for both methods does not seem appropriate. Here are the options that I have come up with:
- List all methods, whether or not the implementation is the same, on the interface. Then use an abstract class for the shared erase() implementation. This seems like be best solution to me, as an
EraseableColoredPencilWILL need to implementerase()and this allows all classes to share the same implementation. I know this is possible, but my concern is whether or not it follows best practices. - Eliminate the interface and use an abstract class. This does NOT seem like it follows good design patterns, but would guarantee that each extending class would have the appropriate methods and even have consistent implementation until a given method is overridden.
- Leave As-Is. It's possible I'm overthinking this and this really is an OK way to do it.
- Something Else. I'm sure I have missed something. Is there a better way to do this?
Iprefix. See oracle.com/technetwork/java/codeconventions-135099.htmldraw()method is different based on color, but ultimately theerase()method just removes the color from the page in this example.