Search code examples
oopinterfaceabstract-classsolid-principles

Should I create an interface for a simple abstract class?


It happens quite often that I have an abstract class that is pretty much an interface, save for a few trivial methods (say an average(array) method).

In these cases, does it make sense to create an interface, so that the abstract class implements it?


Solution

  • You don't need an interface for everything. An abstract class is fine. In some languages you can "implement" several classes and "extend" only one. So only if you have a case where you want to use multiple inheritance, then go ahead and make an interface for it. Otherwise, don't bother and just keep things simple.

    If you use languages like C++, then there is no difference between an interface and an abstract class, so you can see that it doesn't really matter.

    Note that there is also a question about it here: Interface or an Abstract Class: which one to use?