Java Reference
In-Depth Information
Figure 9.3. Single inheritance vs. multiple inheritance
Remember that classes in Java can inherit from only one other class, but classes have always
been allowed to implement multiple interfaces. To confirm, here's how the class ArrayList is
defined in the Java API:
Multiple inheritance of types
Here, ArrayList is extending one class and implementing six interfaces. As a result, an ArrayList
is a direct subtype of seven types: AbstractList, List, RandomAccess, Cloneable, Serializable,
Iterable, and Collection. So in a sense we already have multiple inheritance of types.
Because interface methods can have implementations in Java 8, classes can inherit behavior
(implementation code) from multiple interfaces. Let's explore an example to see how you can
use this capability to your benefit. Keeping interfaces minimal and orthogonal lets you achieve
great reuse and composition of behavior inside your codebase.
 
Search WWH ::




Custom Search