Interfaces
Interfaces are used for implementing Polymorphism.
Interfaces are used for borrowing methods.
Consider this abstract class:
When an abstract class has all methods defined as abstract, then it is used purely for implementing Polymorphism.
Derived class has to implement all the abstract methods to become concrete class.
Interface can be call as a Abstract Class with all abstract methods.
Previous class can be re-written using Interface:
Classes are extended, Interfaces are implemented.
A class can be extended from only one class, and can implement multiple interfaces.
In abstract class, we can create abstract class reference and assign the derived class object.
In Interfaces also, we can create reference of interface and assign the object of class, which implemented the interface.
Interface Example
We can also define our own extra methods in derived class.
Callbacks using Interface
Interfaces are the go-to tool for callbacks because Java doesn’t have function pointers like C or lambdas in older versions.
Interfaces are used for defining callback methods.
Interface Rules
Rule 1
Methods are public and abstract by default in Interfaces.
We cannot make abstract methods in Interfaces as private.
Rule 2
We can have Identifiers in the interface, and they are final and static, but the identifiers must be given name in Upper case.
Rule 3
Methods inside an interface cannot have body, but a method can have body if the method is static.
And we can access the static members using Interface name and dot operator.
Rule 4
We can also have default methods inside the interface (they have their body defined inside interface).
We can override the default method, but if we don't override then also they are accessible.
default methods are useful when, we already have implemented the interface in many classes.
If we want to add one more method in Interface, then all the implementing classes will have to implement that new method as well (otherwise they will become abstract classes).
So, instead we can define that method inside the interface using default keyword, and then we don't need to implement it in all those classes.
Rule 5
We can also define private methods in the interface (private methods that are not abstract).
private methods are useful inside the default method, they are not useful outside the interface.
Rule 6
Interface can extend from another Interface.