1

I'm currently learning more on Java, and have come across an example in a book which I think has made me misunderstand the use of Interfaces. As I see it, the interface in question is completely unnecessary, and I'd like to know if I'm reading it wrong or I should look elsewhere for a better understanding (which I have tried but don't really understand).

The Interface:

public interface Series{
    int getNext();
    void reset();
    void setStart(int x);
}

The implementing class:

class ByTwos implements Series {
    int start;
    int val;

    By Twos() {
        start = 0;
        val = 0;
    }

    public int getNext(){
        val += 2;
        return val;
    }

    public void reset(){
        val = start;
    }

    public void setStart(int x){
        start = x;
        val = x;
    }
}

I don't see what use the interface has here, why the class cannot simply remove the implements statement, and what purpose the interface really serves.

Edit: not identical to duplicate questions since I'm asking about this particular example, and those question answers aren't helping me understand the concept really.

Dant
  • 92
  • 6
  • It means if someone has written a method that accepts a `Series` as an argument, you can pass an instance of `ByTwos` to it. – khelwood Aug 08 '17 at 12:09
  • `Series s = new ByTwos();` is **one** possible use, but what if you need to add `ByThrees()`? Then (with `Series`) you just change to `s = new ByThrees();` and nothing else... without the interface, you have to change more stuff. What if it was a method to `getSeries()`? – Elliott Frisch Aug 08 '17 at 12:09
  • https://stackoverflow.com/questions/240152/why-would-i-want-to-use-interfaces – C-Otto Aug 08 '17 at 12:11
  • @khelwood Ah, I didn't know that was possible. That gives me a little more insight as to why it's useful. – Dant Aug 08 '17 at 12:13
  • @ElliottFrisch That's true, but you'd still need to write the ByThrees class, so why not just create instances of those classes instead of the interface? – Dant Aug 08 '17 at 12:14
  • @Dant because you can then interchangeably use them. If you pass a method a parameter of type series, than it does not matter if the series implementation is of type ByTwos or byThrees. Because the interface is a *contract* that those classes have implemented the interface methods. – JacksOnF1re Aug 08 '17 at 12:21
  • @JacksOnF1re The first part of that I understand now. However, I see the word "contract" used a lot when referencing interfaces, but I really don't understand what it means., so the second part doesn't mean much to me. – Dant Aug 08 '17 at 12:27
  • Contract is used to describe, that if a class implements an interface, it ensures to implement all the methods declared in that interface. It does not matter how the class implement those methods. If a another class knows the interface, but has no clue of the several different other classes that implement the interface, it can still handle all implementation classes, because it "knows" that they are fulfilling the contract and that it can savely call all the interface methods. – JacksOnF1re Aug 08 '17 at 12:35
  • To do so, it will accept the interface as a parameter, like -> void foo(Series s){...}, but you can call the method with foo(byTwos) or foo(byThrees) – JacksOnF1re Aug 08 '17 at 12:35
  • 1
    I think I understand now. So in essence; an interface is a sort of "template" for creating classes, that allows classes made using the template to be more easily interchanged? – Dant Aug 08 '17 at 12:38

1 Answers1

4

TL;DR

  • It defines the "what" i.e. what is implemented
  • It defines the "How" i.e. How to implement it.

The main purpose of the interface is to separate implementation from definition and to enforce that methods are implemented without failure.

Consider that you are a manager and you decide that the programmers should follow a template, You can use Interface.That's what it does, It provides a template. Say a Team leader want to enforce that a certain functionality is implemented but doesn't want to change things in managers template, He can just create another interface. Yes multiple inheritance is possible with interface.

An interface is a contract (or a protocol, or a common understanding) of what the classes can do. When a class implements a certain interface, it promises to provide implementation to all the abstract methods declared in the interface.

It provides abstraction, You can just expose the interface for communication and we can identify the signatures from interface itself.

It also provides a way for implementing polymorphism.

Refer :

Goutham R
  • 56
  • 4
  • 1
    Thank you very much, you've explained it much more easily than other places I've found (also for some reason nobody uses the word "template" which now seems like the simplest term to use to accurately describe it). Also thanks to @JacksOnF1re in the main question comments. – Dant Aug 08 '17 at 12:49