1

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:

  1. 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 EraseableColoredPencil WILL need to implement erase() 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.
  2. 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.
  3. Leave As-Is. It's possible I'm overthinking this and this really is an OK way to do it.
  4. Something Else. I'm sure I have missed something. Is there a better way to do this?
3
  • If you are coming from a C# perspective, that is the case, yes. I have had this problem in both Java and C#, and Java naming conventions do not call for the I prefix. See oracle.com/technetwork/java/codeconventions-135099.html Commented Jul 16, 2015 at 11:57
  • I tried to simplify this question for the sake of the website, when in reality it is a much more complicated class. The idea behind this is that the draw() method is different based on color, but ultimately the erase() method just removes the color from the page in this example. Commented Jul 16, 2015 at 12:00
  • Sorry about that, have had the problem in multiple languages, but I removed the tag for now. Commented Jul 16, 2015 at 12:06

2 Answers 2

2
  1. Something else: Follow the Interface Segregation Principle and split the interface:

    interface Drawer{
      void draw();
    }
    
    interface Erasable {
      void erase();
    }
    
    interface EraseableDrawer extends Drawer, Erasable {
    }
    

    Now you only need to depend on either Drawer or Erasable, depending on what method you really need (or on ErasableDrawer if you need both).

    If erase() is really the same implementation for all or most classes, you still can use an abstract class AbstractErasableDrawer that implements ErasableDrawer with a concrete implementation of erase() (or use a default implementation as suggested above)

Sign up to request clarification or add additional context in comments.

Comments

1

As you are now just asking with regards to Java, there is a third option for you, available with Java 8: default methods on interfaces.

This allows you to use an interface and define the default behaviour for erase, removing the need for the abstract class:

interface EraseableColoredPencil {

    void draw();

    default void erase() { ... }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.