0

In all the new applications I've built from scratch, I've never seen the need for an interface, I've read up in the past in text and subsequently ignored and forgotten the real need.

That said, here is a question I've seen asked in the past, which I'll use to ask this particular question by example...

Lets say I have this interface:

public interface IBox
{
    public void setSize(int size);
    public int getSize();
    public int getArea();
    //...and so on
}

And I have a class that implements it:

public class Rectangle implements IBox
{
    private int size;
    //Methods here
}

Used as follows:

public static void main(String args[])
{
    Rectangle myBox=new Rectangle();
    // do stuff.
    System.out.println( myBox.getSize() );
}

Given that I still have to write the setSize getSize getArea methods in the Rectangle class. Given that Rectangle with these methods, would still work even without the IBox interface.

What is the point of having the Ibox interface other than to publicize or dictate or describe the setSize getSize getArea methods?

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
  • Well, interfaces enable something called *polymorphism* – ewernli Sep 16 '13 at 19:48
  • 1
    This question adds up to "what are interfaces good for?" http://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface – Dave Swersky Sep 16 '13 at 19:49
  • A google search would have produced millions of hits – user1231232141214124 Sep 16 '13 at 19:49
  • There are many good resources for this question both on SO and the internet in general. Google is your friend. – Aurand Sep 16 '13 at 19:50
  • 2
    I understand votes to close, but why the downvotes? This isn't an intuitive question, first exposure with the subject may be confusing. – Sergey Kalinichenko Sep 16 '13 at 19:51
  • Thanks Dave. That link had the best example I've seen. Contrary to popular belief I had researched this before. AND I now understand why you would use interfaces (only took ten years). I'd give a +1 to you and the author in the said link if I had enough rep points. – Frank Develops Sep 17 '13 at 23:00
  • I agree with Frank on this. Sure google will produce a million hits. Problem is that its mostly the same pieces/explanations copied and repeated over and over. No one really offers a different or new explanation into the subject. If you dont understand the first explanation you dont even need to bother with the other 999999 because they add nothing new. People might disagree with me, but thats the way its perceived in practice. Its seems to be a common phenomenon that interfaces is a hard concept to grasp. – Chris Dec 22 '14 at 08:53

2 Answers2

2

Well, let's not answer it directly first. Let's say that we have a static method scaleBox() that takes a box and scales it. Obviously HexagonalBox won't extend Rectangle. We'd need a scaleBox(HexagonalBox h) and a scaleBox(Rectangle r). We can have both of these boxes implement IBox and have one method, scaleBox(IBox box). That method is guaranteed a setSize().

Since an interface is a type, you can have many classes that don't extend each other implement that interface, and get passed safely(as they are guaranteed to obey the contract that is expected when accepting an interface as a parameter)

On a related note coming up for JDK 8 next year, you'll need specially-designed interfaces in order to use the lambda feature in that new release.

nanofarad
  • 40,330
  • 4
  • 86
  • 117
1

Your example is fine. What you left out was what would happen if you wanted your code to work with other types of boxes. If you hard code for Rectangle, it would be near impossible to add other specifications such as Square (assuming Square is specialized enough to have it's own implementation). Think of the following:

public void DoSomethingWithBox(Rectangle box)
{
    // Do something with the box.
}

Now, you'd have to write an overload to take a square if you wanted. Or, you could simply have both Rectangle and Square implement the interface which means you could change your single method to:

public void DoSomethingWithBox(IBox box)
{
    // Do something with the box.
}

And it can now be called by anything that implements your IBox interface.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536