Summary
We started this chapter by examining abstract
classes. An abstract
class has zero or more abstract
methods. However, if any method is abstract
, then the class must be abstract
. While an abstract
class cannot be instantiated, a reference can be of an abstract
type.
Before Java 8, interfaces consisted of only abstract
methods (and constants). We started our discussion on interfaces at this point, where all the methods were abstract
. While a class can only extend from one class, a class can implement many interfaces. This is one of the main reasons why interfaces were introduced – to be able to cast to more than one base type.
A class that implements an interface signs a “contract” to provide code for each of the abstract
methods (if any) in the interface. If there is an abstract
method in the interface and the concrete, non-abstract class does not provide code implementation for it, the compiler complains. Therefore, interfaces are a great way of guaranteeing...