The concept of interface is a part of abstraction, one of the four OOP. characteristics. Abstraction is about an abstract design of common features, including operations of the object.
Interface is the blueprint of a class. However, it is neither a class nor an object. All methods defined in an interface are abstract. There are no implementation details allowed inside any method of the interface. The class that is going to implement the interface will take care of the actual implementation of the methods.
As two common types of automobiles, cars and buses are common objects. Both cars and buses share the same type of behaviors defined in Auto interface. When we create a class for cars and buses, we use the keyword implements to implement car and bus from the same Auto interface with different behavioral details.
An interface indicates what the object can do.
When a class implements the interface, it defines what the object is doing with the necessary details.
When we want to only specify the behavior of a particular data type, without being concerned about whoever implements its behavior.
For example:
We define an interface Auto that is an abstract concept and a general term. In this interface, we define several methods such as start, stop, turn, back, and park by their signatures, without any implementation details. We will add implement details in these methods when we create Car or Truck classes that implement the Auto interface.
When all classes have the same structure, but they totally have different functionalities.
For example:
Dogs and cats communicate in totally different ways. A dog barks, but a cat meows. We may define an interface Animal and create a class Dog and class Cat, like shown here.public interface Animal {public void communicate();}public class Dog implements Animal {public void communicate() {System.out.println("bark, bark!");}}public class Cat implements Animal {public void communicate() {System.out.println("meow, meow...");}}