Interface is another way to achieving the abstraction.
Interface is a blueprint of class.why blueprint?
As class is a blueprint for an object class is a collection of datamember and function and we save it Test.java compile and run and bytecode of the class is also generated as well as interface is save as Test.java run and compile same and byte code is also generated .
Interface is a contract between 2 party programmer and programming language.
In java every work where java wants you to define method and java calls method and declare method and method will be executes yours for this java creates interface.via interface we can achieve dynamic bind Ing,achive run time polymorphism,multiple inheritance.when in project level we have only requirement then create an interface and keep it.fro java 8 in interface static method may be there beacuase static method is statically bind and interface dynamically bind and we can have main method also.
Sol:
Interface I {
Void show() ;
}
class A implements I {
public void show() {
}
}
If class A is implements interface then it has to implements all the method present in interface with public access privilleage if class don’t Want to implement methods then make it abstract .we can’t create an object of an interface.
Sol: Yes by using comma, but this is not called multiple inheritance its called multiple implements keyword.
Sol: Yes by using extends keyword its an example of inheritance in interface We can not declare variable inside interface we must have to initialize it.
Interface I {
//int x; we can’t declare
int x=10;
void show();
static void disp() {
}
}
class A implements I
{
public void show() {
}
}
Note: We can’t invoke the child personal mehod with parent reference. Any interface can’t extends any class but extends any interface. Object class is the parent class of all the classed but not for interf Interface can’t implements any class not extends any class.and Any class can’t extends interface implements interface. And a interface extends more than 1 interafce simultaneously separated by comma this is the multiple inheritance in interface in java
It is called Multiple implementation of a Class but not multiple inheritance
When an interface extends more than 1 interface Is called multiple inheritance though interface in java illustrate example
Sol: Generally people say no ya offcoarse no but lets see something
Now Question we don’t want to implements an interface but we want To override method forcefully so for this also we use anonymous class to override method compulsory in some real life if we wish to pay from Phone pe then we necessary have phonepe in our mobile to transfer money.its a fix condition we want from everybody