HTML Java

Java Interface


Interface

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.



Ques: How a class uses interface ?

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.



Ques: Can a class implements more than 1 interface?

Sol: Yes by using comma, but this is not called multiple inheritance its called multiple implements keyword.



Ques: Can interface extends another interface?

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() {
}
}



All about interface.

Example

interface Interface {
// we can't declare variable we must initialize it int x;
int x=10;
// we can't provide method dfinition here void show(){ }
void show();
void hi();
// we can have static method
static void disp() {
System.out.println("disp of interface");
}
}
// if we are implementing class we have to define all the method in class A other wise
// make abstract class A implements Interface
abstract class A implements Interface {}
class B implements Interface {
public void show(){
System.out.println(" class B show");
}
//if we are not defining method hi() also of Interface then we get compile time error
//all method of interface defines wtih public access privilleage
public void hi() {
System.out.println(" class B hi");
}
}
class Test {
public static void main(String args[]) {
Interface i=new B();
i.show();
i.hi();
}
}
Try it »

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



Example of a class implements 2 interface

It is called Multiple implementation of a Class but not multiple inheritance

Example

abstract interface i1 {
void show();
}
abstract interface i2 {
void disp();
}
class A implements i1,i2 {
public void show() {
System.out.println("show A");
}
public void disp() {
System.out.println("Disp A");
}
}
class MIClass {
public static void main(String args[]) {
i1 o1 =new A();
i2 o2=new A();
o1.show();
o2.disp();
}
}
Try it »


Example of an interface extends more than 1 interface

When an interface extends more than 1 interface Is called multiple inheritance though interface in java illustrate example

Example

interface i1 {
void show();
}
interface i2 {
void show(); //here we are creating another show method when we have in interface i1 and when I extends i1,i2 then why its not conflict
//and when we creata a same name method in normal class then start ambiguity problem why becoz here is only declaration
void disp(); //we don't need to define 4 methods in class A we have 2 define only 3 in class A
}
interface I extends i1,i2 {
void hi();
}
class A implements I {
public void show() {
System.out.println("show A");
}
public void disp() {
System.out.println("Disp A");
}
public void hi() {
System.out.println("Hi A");
}
}
class MInherit {
public static void main(String args[]) {
I o =new A();
o.show();
o.disp();
o.hi();
}
}
Try it »


Ques: Can we create an object of an Interface?

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

Example:

Interface I {
void show();
public String toString();
}
class Test implements I {
public void show() {
System.out.println(“ Show“)
}
public static void main(String args[]) {
I i =new Test();
i.show(); // it will give error because we can’t call child method with parent reference but when we write
i.toString it will compile how this line when this methos id not declare
in interface then you think that interface extends Object Cla
ass but interface can’t extends class so how its working. Its
working when we compile the program compiler will declare the methods Of object class not extends Object class that’s why and every class extending Object class internally so method is internally will be present in Class Test and Deinition is in Interface so this working like
this. Not a magic. We are only override only 1 method.
}
}