An interface is an abstract class that is used to group related methods with empty bodies, Method bodies exist only for default methods and static methods. Interfaces cannot be instantiated—they can only be implemented by classes or extended by other interfaces
- It is used to achieve loose coupling which means dependencies of a class that uses another class are highly reduced. In simple the main class has access to the classes that are exposed through the interface
- Interfaces are used to implement abstraction. The reason is, that abstract classes may contain non-final variables, whereas variables in the interface are final, public, and static.
- Interface can used to implement Inheritance
Syntax for interface
Syntax to declare interface is as follows
Interface is the keyword used to declare it and all fields are public, static, and final by default. To implement an interface implement keyword is used. A class can implement more than one interface. An interface can extend to another interface (but only one interface). A class that implements the interface must implement all the methods in the interface.
Output: