Interface:
A : Abstract class
B : Abstract class
If we want to declare the definition of method of A and B in C (Another class)
class C extends A, B. // Multiple Inheritance , Java does not support Inheritance
In order to support Multiple Inheritence, Interface is used
In abstract class, method can be declared and defined also
In Interface, method can be only declared
If A and B are interface then
C can be implemented like
class C implements A, B
Interface A
{
public void show();
}
Class B implements A
{
public void show()
{
System.out.println(“Hello”);
}
public void display()
{
System.out.println(“Hi”);
}
}
B obj = new B();
Then obj.show() and obj.diplay() can be called, So, object can use show and display method
But, If we wish to restrict user to use only show
Object of interface can not be created but it can be refrenced.
A obj = new B(); // obj is reference of A and giving memory of B
obj.show(); // show belongs to A
obj.display(); // through error as display does not belongs to A, it belongs to B
This is the main reason why interface is largely used
0 comments:
Post a Comment