1

I'm trying to wrap my head around interfaces and I've been reading a lot of similar questions/answers on the topic, but it's still not clicking with me. I understand what an interface literally is, but I don't get why you would ever use it.

So an interface contains a bunch of empty functions, and you then implement that interface in a class. But what I don't get is, you still have to write a full function declaration for any function you take from the interface. So say you have a function 'void printHello()' in your interface, and you implement that interface in a class. You still have to write:

public void printHello() { 
    System.out.println("Hello!");
}

which is a complete function declaration. You could delete the 'implements interface' command and literally nothing would change, you would still have a working function 'printHello()'. So functionally speaking, isn't the interface basically doing nothing? To go further, you could have another class that also implements the same interface, but you could make the same function do something completely different:

public void printHello() { 
    System.out.println("Bababooey!");
}

So it's not like it's pre-defining functions so you can call them anywhere like a class/object would do. If this is the case what is the interface really providing?

  • For cases where the implementation is the same across all classes, you can use the default method - https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html. – mre Jul 01 '20 at 19:37
  • 4
    "If this is the case what is the interface really providing?" A contract. – Kayaman Jul 01 '20 at 19:41
  • Since Java doesn't have structural typing, it lets you have a method that can accept all kinds of objects as long as they adhere to some contract. For example, you could have all kinds of classes define a `compareTo` method without implementing `Comparable`, but then you'd need a separate method for each of them.You could always have a class called `Comparable`, but you can only extend one class, whereas you can implement as many interfaces as you want – user Jul 01 '20 at 19:45
  • Also, in Java 8+, you can have default methods in interfaces to avoid code duplication – user Jul 01 '20 at 19:46
  • Also see [What is the difference between an interface and a class and why should I use an interface when I can implement the methods directly in the class?](https://stackoverflow.com/questions/10914802/what-is-the-difference-between-an-interface-and-a-class-and-why-i-should-use-an?rq=1) – user Jul 01 '20 at 19:54

1 Answers1

1

By using interfaces you outsource the responsibility of logic implementation to the classes which implements that interfaces, this is called API. Lets say you have a List which is an interface and which is implemented by a bunch of different classes like LinkedList or ArrayList. By using List you are flexible to utilize any implementation of that List which varies on your particular needs and simply switch to a different one later on, whereas you code remains the same -- you still use List. So doing an add method you are not interested or you don't care how it is done, you just want to add an element to the collection and how it is implemented internally it is not your business.

jwpol
  • 1,188
  • 10
  • 22