Abstract Classes & Interfaces
Abstract Classes
Abstract classes are designed to be extended and cannot be instantiated.
For example, an abstract class Person might contain common attributes and
rely on concrete subclasses like Employee and Student
to provide additional
functionality. An abstract class can have both concrete and abstract methods,
but it can only be instantiated by a concrete subclass that implements every
abstract method somewhere along the object hierarchy.
Interfaces
Interfaces promote polymorphism by describing a form that an object can take.
For example, imagine a government agency that is interested in taxing people.
Rather than tightly couple their codebase
to the Student and Employee
objects, they could instead introduce a Taxable interface and require people
to provide their own implementations. Interfaces can only contain constant fields
and empty methods. A class can implement
more than one interface, but it must
provide the implementation for every method (duplicate methods are treated
as one).