Interface
________________
________________
Interface is nothing but 100% pure abstract class, it has static constraints and abstract methods, In Java interface is the mechanism to achieve abstraction and multiple inheritance. interface also represents the IS-A relationship, it cannot be instantiated just like the abstract class.
Since Java 8, we can have default and static method in an interface
Since Java 9, we can have private methods also in an interface.
There are mainly three reasons to use interface.
we can declare an interface by using interface keyword and implement by using implements keyword
If a class implements an interface compulsory we have to provide implementation for every method of that interface otherwise we have to declare class as abstract, violation leads to compile time error. whenever we are implementing an interface method compulsory it should be declare as public otherwise we will get compile time error.
whether we are declaring or not, every interface method is by-default, public and abstract
interface Itf {
void m1()
}
interface method specifies requirements but not implementation, hence the following method declarations are equal inside interface.
void m1();
public void m1();
abstract void m1();
public abstract void m1();
as every interface method is by-default, public and abstract the following modifiers are not applicable for interface method. private, protected, <default>, final, static, strictfp, synchronized, native
An interface can contain variable the main purpose of these variable is to specify constant at requirement level. every interface variable is always public, static and final whether we are declaring or not
interface Itf {
int x = 11;
}
public - To make this variable available for every implementation class.
static - without existing object also implementation class can access this variable
final - implementation class can access this variable but can't modify
For the interface variable we must perform initialization at the time of declaration only otherwise we will get compile time error.
case 1: If two interface contains method with same signature and same return type in the implementation class we can provide implementation for only one method
case 2: If two interface contains method with same name but different arguments then in the implementation class we have to provide implementation for both methods and these methods are considered as overloaded methods.
case 3: If two interface contains method with same signature but different return types then it is impossible to implement bot interface at a time, we can't write any java class which implements both interface simultaneously.
It is possible a java class can implement any number of interface simultaneously, except two interface contain method with same signature but different return types.
If an interface don't contain any method and by implementing that interface if our objects will get ability such type of interface known as marker interface, tag interface, ability interface.
eg. serializable, clonable, RandomAccess, Single thread mode - these interfaces marked from some ability.
A. JVM is responsible to provide required ability to marker interface
A. To reduce complexity of the programming
A. Yes, but customization of JVM is required
Adapter class is a simple java class that implements an interface, an interface only with empty implementation.
Interface :
abstract :
A. abstract class constructor will be executed whenever we create child class object to perform initialization of parent class instance variable at parent level only, and this constructor meant for child object creation only.