An interface is a service requirement specification (srs). From the perspective of the customer, an interface defines the set of services that he expects. An interface defines the collection of services that a service provider offers from the perspective of the service provider. As a result, an interface can be thought of as a contract between a client and a service provider.
Example: The ATM GUI screen defines the set of services that the bank is offering, while also describing the set of services that the consumer is expecting, thereby acting as a contract between the bank and the customer.
Because every method in an interface is always abstract, whether we declare it or not, interface is regarded as a pure abstract class.
An interface is a reference type in Java. It's similar to taking a class. It is made up of a collection of abstract procedures. When a class implements an interface, it inherits the abstract methods from the interface.
In addition to abstract methods, an interface can have constants, default methods, static methods, and nested types. The bodies of default and static methods are the only ones that have them.
In the following ways, an interface is analogous to a class:
Any number of methods can be included in an interface.
An interface is written in a .java file with the same name as the file.
An interface's byte code is stored in a.class file.
Interfaces exist in packages, and their bytecode files must be in the same directory structure as the package name.
An interface, on the other hand, differs from a class in various aspects, including:
You cannot instantiate an interface.
There are no constructors in an interface.
In an interface, all of the methods are abstract.
In an interface, instance fields are not permitted. The only fields that can be used in an interface must be static and final.
Instead than extending an interface, a class implements it.
A single interface can extend many interfaces.
An interface is declared with the interface keyword. By default, all fields in an interface are public, static, and final, and all methods in an interface have an empty body. All of the interface's functions must be implemented by a class that implements the interface.
It's a method for completely abstracting something.
Because multiple inheritances in the context of classes are not allowed in Java, multiple inheritances can be obtained by utilising an interface.
It's also suitable for loose coupling.
Abstraction is implemented through interfaces.
A class implements an interface in this way. It must provide the body of all interface methods, or in other words, the class must implement all of the interface's methods.
1. We can accomplish implementation security without worrying about the implementation component.
2. Several inheritance is not allowed in Java, but you can use interfaces to do so because you can implement multiple interfaces.
3. Polymorphic behaviour is primarily provided by interfaces.
4. Interfaces are used to break apart complex designs and make object dependencies explicit.