-1

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.

user3366706
  • 1,529
  • 3
  • 31
  • 54
  • Usually it's so you can create `class OtherInterfaceObject implements A` which does something different when `display()` is called. – markspace Oct 05 '14 at 02:10
  • _What is the advantage of creating interface object?_ You can't create an interface object. You can define a reference variable whose type is an interface. – Voicu Oct 05 '14 at 02:13
  • Interfaces allow freedom from Inheritance and the Class Hierarchy. Use them. – user2864740 Oct 05 '14 at 02:14
  • Look at, eg, [LinkedList](http://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html). It inherits Deque, List, and Queue interfaces, allowing it to play different roles depending on the circumstances. You can't do that with a single common inheritance chain. – Hot Licks Oct 05 '14 at 02:19
  • I couldn't see similar question in SO. Can anyone post the link of a similar question so that i can edit the question if necessary? – user3366706 Oct 05 '14 at 02:53
  • 1
    It is true that interfaces can be used to obfuscate and confuse, and some folks like to overdo them in that regard. It should also be noted that, at least when interpreted, interface method calls are less efficient that regular virtual method calls. – Hot Licks Oct 05 '14 at 03:02

1 Answers1

0

The classic illustration of an interface (or an abstract type) is that of a shape. Let's say you are writing a program that displays shapes -- triangles, rectangles, circles, etc. All of them can implement certain methods that allow them to be displayed on the screen -- say, a display(screen) method -- so that each shape knows how to do that for itself.

But by implementing the 'shape' interface, and treating objects as Shapes, code handling shapes does not have to know about handling each type of shape, only about those methods common to shape. That's the advantage. A display portion of the program can have a collection of these and deal with them without having to know their individual characteristics.

This kind of generality is applicable to many situations.

arcy
  • 12,845
  • 12
  • 58
  • 103
  • A Shape could be a Class and still show said example - so why use Interfaces? – user2864740 Oct 05 '14 at 02:15
  • 1
    Java does not have multiple inheritance, so making Shape an interface leaves the implementing class (Traiangle, Rectangle, etc.) free to extend another class, and still implement Shape. If Shape were a class, the implementing class would have to extend Shape, and could not extend any other class. The interface also produces looser coupling - other classes dealing with Shape implementations don't need to know what the specific class of the implementer is. – GreyBeardedGeek Oct 05 '14 at 02:19