Why does Java use interfaces?
- There is no support for multiple inheritance in Java, that is, a subclass cannot inherit multiple parent classes, and of course you ask why. Because this causes uncertainty in some methods or variables in subclasses.
- In order to compensate for the shortcomings of the single inheritance in [1], Java appeared the concept of interface, the interface can be very good to avoid the emergence of this problem.
- In Java, there are many ways to implement interfaces to solve the problem of multiple inheritance between classes.
What is an interface?
- In the Java programming language, an interface is not a class, but rather a set of requirements descriptions for a class that is defined in accordance with the uniform format of the interface description. An interface has only the characteristics of methods , and there is no implementation of methods , so these methods can have completely different behavior when implemented in different places.
- A class can implement one or more interfaces.
features of the interface:
- An interface is not a class, especially an interface that uses the new operator instance.
- An object that can declare an interface's variables, but cannot construct an interface. (appearing in callback design mode, which embodies the polymorphic nature of Java).
class implements the interface steps:
1) Declare the class to implement the given interface.
2) Define all the methods in the interface.
The keyword that implements the interface is implements:
Java Interface: interface