-2

I'm very new into Java and I've reached the chapter in my textbook that talks about interfaces. However, I am failing to see their use/point. I'm sure that they're not useless though and that it's just me not understanding the subject.

So, interfaces are like abstract classes and they only name methods, not their implementations. The first example provided in the book talks about having a class Shape, an extended class rectangle and the idea is to allow for a method to stretch the rectangle by its biggest side. So the code starts:

public interface Stretchable {
    void stretch( double factor );
{

Then they describe the "point" of using it

public class Rectangle extends Shape implements Stretchable
{
   public void stretch( double factor )
   {
      if( factor <0 )
      throw new IllegalArgumentException();

      if( length > width )
          length *= factor;
      else
          width*= factor;
   }
}

However, I fail to see what did I win there, by defining and implementing an interface. the interface was just void stretch( double factor); and I still had to define the same thing in the Rectangle class. What is the advantage in doing this, when I still have to define from scratch what the method of the interface is going to do in the class it's being implemented on?

Andronicus
  • 25,419
  • 17
  • 47
  • 88
Dasphillipbrau
  • 524
  • 2
  • 8
  • 17
  • What if other things are also stretchable? What if a collaborator can operate on anything stretchable and doesn't want to depend on specific implementations? See also [the questions linked to the proposed dupe](https://stackoverflow.com/questions/linked/504904?lq=1), [this cross-site question](https://softwareengineering.stackexchange.com/questions/131332/what-is-the-point-of-an-interface) and any decent intro to strongly typed languages. – jonrsharpe Feb 17 '20 at 17:30
  • 1
    Call it a “contract” and it may make much more sense to you. – Thorbjørn Ravn Andersen Feb 17 '20 at 17:35
  • For starters, Java does not allow multiple inheritance (basically because [diamond problem](https://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem)) so you cannot extend multiple classes. You can implement as many interfaces as you want with no such problem. – Michael Feb 17 '20 at 17:38
  • Interfaces are valuable to Java because they allow a developer to specify an API independently of its implementation. E.g. the `Collection` interface in the Java Collections Framework specifies the API used by many classes that implement it: `HashSet`, `ArrayList`, `TreeSet`, `SortedSet`, etc. – scottb Mar 21 '20 at 19:29

1 Answers1

1

Because you define a common behavior for all classes, that can be stretched (Stretchable). Say you have a Circle:

public class Circle extends Shape implements Stretchable
{
   public void stretch( double factor )
   {
      this.radius = this.radius * factor;
   }
}

Now if you can have a collection of all the things, that can be stretched, because there is a common interface:

List<Stretchable> stretchables = List.of(new Circle(1), new Rectangle(1, 2));

Since they are all Stretchable, you can invoke the method on all of the objects:

stretchables.forEach(stretchable -> stretchable.stretch(2));
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Andronicus
  • 25,419
  • 17
  • 47
  • 88
  • But I still had to define what Stretch did for every class and the implementation for each one was different, so in the end what was the difference between using that interface (which is just naming a method) and just adding my method manually to my extended class? At the end of the day the only thing that I took from the interface was the method's name, which I still had to write from scratch. – Dasphillipbrau Feb 17 '20 at 17:41
  • 1
    So that you can invoke those two last snippets. Without the interface, there will be a compilation error. – Andronicus Feb 17 '20 at 17:42