I see people creating an instance of some class and assigning it to a reference variable of type interface that the class implements.
interface A {
void display();
}
public class InterfaceObject implements A {
public void display(){
System.out.println("show this..");
}
public static void main(String[] args) {
A aObj = new InterfaceObject();
aObj.display();//OUTOUT:show this..
InterfaceObject bObj = new InterfaceObject();
bObj.display();//OUTOUT:show this..
}
}
Here the object aObj is an interface object and the object bObj is a direct instance of the class implementing the interface. However the call display() through both the objects yeild same result.
QUESTION: What is the advantage of creating interface object(reference variable of interface type)? does it only add more confusion to the code? ofcourse, that will not be the case.