Interface or abstract class
There is always a debate over using either an interface or an abstract class. Here are a few rules to follow when deciding which way to go:
Is-a versus Can-Do: Any type can inherit from one parent class only and multiple interfaces. If for the derived class B you can't say B Is-an A (A is the base type), don't use an interface but rather an interface. Interfaces imply a Can-Do relationship. If the Can-do functionality is applicable to different object types, go with an interface implementation. For example, for both
FileOutputStream
andByteOutputpuStream
(and any of the other sibling implementations available), you can say they have an Is-a relationship withjava.io.OutputStream
. Hence you will see thatOutputStream
is an abstract class providing common implementations to all objects that represent a writable stream. However,Autocloseable
, which represents an object holding a resource that can be released when theclose
method is invoked, provides aCan-do
functionality...