Interfaces
- Instances of classes not related by inheritance may still be used interchangeably if the classes all also implement the same Java interface.
- A class can implement multiple Java interfaces and instances can therefore be used in multiple ways.
- However, a class that implements an interface must provide implementations of every method in the interface.
Abstract Classes
A class is abstract if it has at least one abstract method. An method declared as abstract has no implementation. Implementation of abstract methods are the responsibility of subclasses.
Example
public abstract class AbstractList<E> { ... public void add(int index, E element) { ... } // Not abstract; implemented ... public abstract E get(int index); // Abstract; no implementation in this class ... }
- Instance of classes that are subclasses of the same abstract class may be used interchangeably.
- A subclass of an abstract class only has to implement the abstract methods; it can simply inherit the implementations of non-abstract methods (like less work for the programmer).
- However, a class can inherit from only one class; in particular, a class can't inherit from multiple abstract classes.