Java Interface is similar to a class that is capable of handling multiple inheritance. It is nothing but a collection of abstract methods. In class, multiple inheritance is not possible. To remove this disadvantage, we use an interface that is capable of handling multiple inheritance. An interface can have methods and variables just like the Java class but the difference is that the methods declared in the interface are by default abstract. The variables declared in an interface are public, static & final by default, too.

Why use the interface in Java

Java Interfaces do not have a body; they have to be implemented by the class before you can access them. The class that implements the interface must implement all the methods of that interface. Also, the Java programming language does not allow you to extend more than one class. However, you can implement more than one interface in your class.

Java Interface Example

If we want to declare an interface, we have to the interface keyword. It is used to provide the total abstraction. That means all the methods in the interface are declared with an empty body and are all the fields are public, static and final by default. A class that implements an interface must implement all the methods declared in the interface. If we want to implement an interface, we have to use the implements keyword.

#java #java interface #programming

Java Interface Example | Interface in Java Tutorial
2.70 GEEK