In this section we will cover the following :

  1. What are interfaces and what are they used for?
  2. Declare interfaces
  3. 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?

  1. An interface defines a contract. Interfaces guarantees that if any class implements the interface, the classes are guaranteed to have those methods available.
  2. Interfaces allow multiple inheritance in Java. In Java a class can only inherit from one parent class.
  3. Interfaces decouples the definition from its implementation. The concept is similar to C++ header definition and implementation which are defined in separate files.

results matching ""

    No results matching ""