Definition:
In java programming language, an interface is an abstract, conceptual or concrete type that is used to specify an interface.
How to use an interface:
It means that, using interface, we can specify what a class must do, but not how it does it.
Interfaces are syntactically similar to classes, but they do not instance variables, and their methods are declared without any body. I.e. methods with empty body.
Structure of an interface:
The structure of an interface is much like similar to that of a class.
Below is the general form of an interface:
Access specifier for an Interface
Access specifier for an interface is either public or it is not used.
Methods for an interface
Notice that, the methods which are declared for an interface will have no bodies.
Variables for an interface
Variables declared inside an interface must possess the following properties:
Note: All methods and variables are implicitly public if the interface, itself, is declared as public.
How to implement an interface via class
Once interface is defined, any number of classes can implement an interface.
Also, one class can implement any number of interfaces.
To implement an interface,
“A class must create the complete set of methods defined by the interface.”
Interfaces are designed in such a way that they support dynamic method resolution at run time.
Interfaces have a different set of hierarchy from classes, thus, it is possible for classes that are unrelated in terms of the class hierarchy to implement the same interface.
This is where the real power of interfaces is realized.
Example of an interface
Below is an example of an interface that declares a simple interface which contains one method called postback( ) and that takes a single integer as a parameter.
Once an interface has been defined, one or more than one class can implement that interface.
For implementing an interface, it includes the implements clause in a class definition, and then creates the methods defined by the interface.
The general form of a class that includes the implements clause looks like this:
Below is an example of a small class that implements the postback interface:
Variables in interfaces a more look
We can use interfaces to import shared constants into multiple classes by simply declaring an interface that contains variables which are initialized to the desired values.
When we include that interface in a class (that is, when we “implement” the interface), all of those variable names will be in scope as constants.
If an interface contains no methods, then any class that includes such an interface doesn’t actually implement anything.
Interface can be extended from another interface
If we want to inherit an interface from another we use the keyword extends.
The syntax of extending an interface is the same as for inheriting classes.
When a class implements an interface that inherits another interface, it must provide implementations for all methods defined within the interface inheritance chain.