Java Interfaces

In Java programming, an interface is a collection of method declaration and constant that one or more class can use.

It is syntactically similar to the class but the main difference between class and interface is that class contain method declaration with method body and interface method declares without any body.

Syntax of interface:

  • Access modifier is always public which indicates that the interface can be used by any class.
  • To declare the class as an interface, interface keyword is used.
  • Variables declares inside the interface are implicitly final and static. They can’t be changed in the implementing class.
  • All methods in the interface are the implicitly abstract method.
  • The main difference between class and interface is that class contains method with method body and interface contains only abstract method.

Implementing an Interface

Once the interface has been defined, one or more classes can implements the interface and one class can implements more than one interface.

Syntax for implementing interface:        

Multiple inheritance: Java dose not support multiple inheritance.

java Multiple inheritance using interface
java Multiple inheritance using interface

Let A and B are parent class and C is the derived class. In Java, instead of multiple inheritances we use interface.

Result