Interfaces
In this section we will cover the following :
- What are interfaces and what are they used for?
- Declare interfaces
- Implement interfaces
What are Interfaces
Interfaces are 100% abstract classes. Interfaces define method definitions but does not provide the implementation. Each class that implements
the interface must provide its own implementation.
An interface is like a contract. The methods it defines are mandatory and any class that implements the interface should provide the implementations.
What are interfaces used for?
- An interface defines a contract. Interfaces guarantees that if any class implements the interface, the classes are guaranteed to have those methods available.
- Interfaces allow multiple inheritance in Java. In Java a class can only inherit from one parent class.
- Interfaces decouples the definition from its implementation. The concept is similar to C++ header definition and implementation which are defined in separate files.