-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

Programming Kotlin
By :

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
and ByteOutputpuStream
(and any of the other sibling implementations available), you can say they have an Is-a relationship with java.io.OutputStream
. Hence you will see that OutputStream
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 the close
method is invoked, provides a Can-do
functionality...