0

So, according to GeeksForGeeks, an interface may have fields and method signatures, but those methods cannot be defined.

A class that implements an interface must then define the body of every method in the interface it implements.

If this is the case, what is the point of the method signatures in the interface? Why not allow oneself wiggle room rather than restricting yourself to have to define methods that you may or may not want to use?

Andronicus
  • 25,419
  • 17
  • 47
  • 88
rocksNwaves
  • 5,331
  • 4
  • 38
  • 77

3 Answers3

1

That's not always true. You can provide a default implementation in the interface:

interface SomeInterface {

    default int combine(int a, int b) { return a + b; }

}

Now the method implementing SomeInterface can override combine method, but it doesn't have to. In that case it falls back to the default implementation.

Andronicus
  • 25,419
  • 17
  • 47
  • 88
  • Ok! That's cool, thank you! But, it leaves the essence of my question unanswered. Why would you ever only want to write a signature and not a definition in the interface? In that case an implementing class would have to define the method body anyway. – rocksNwaves Feb 06 '20 at 18:15
  • Having interface provides us the way to define a behavior. Let's say you have two classes implementing `SomeInterface`: `A` and `B`. You can instantiate some variable of type `SomeInterface`: `SomeInterface a = new A()`; and invoke the method. Same thing would be with `SomeInterface b = new B()`. So no matter what implementation you're using `A` or `B` you can always invoke the method, because they have the same behavior. – Andronicus Feb 06 '20 at 18:19
1

Defining methods in the interface allows you to get all benefits of polymorphism. For example, if you have a List, you don't know what exact implementation of List was used to create it - whether it's ArrayList or LinkedList or something else, but you know that you can, for example, add elements in it and get elements from it, because methods add and get are defined in the List interface.

ardenit
  • 3,610
  • 8
  • 16
1

Well, there are two points in that.

First, Java supports an implementation in interfaces. You need to use the default keyword, which is in Java since Java 9 (I am not quite sure about the version number). But, why would you do that?

Interfaces share a common interface for several classes and can therefor be used as data types. You can for example write a method, which needs a parameter of an interface type. Within the method you can then call all the parameters methods, where you know their signature, based on the interface. The point here is, that interfaces describe common behavior. That is, what interfaces are for!

The difference between (abstract) classes and interfaces is: in (abstract) classes, you define, what you have and what it will look like. Inheritance in this case then is a relation of extension (or spezialization), so you describe it in form of the child IS a parent, but it may have something more. But the IS relation is the basic point. Interfaces describe the behavior, so a class that implements a interface acts like that interface. LinkedList and ArrayList are quite good examples. Internally they look quite different, but they both store many elements (as hash lists do too). Both classes implement the List interface, because you can both treat them as lists: you can for example iterate over them, which is not the case in hash maps.

Thus, if you want to share common structure and content, use inheritance and maybe abstract classes to group them. If you want to share common behavior, use interfaces, because it doesn't matter, how they look inside, but what you can do with them. So you group it by action.

Jochen
  • 193
  • 1
  • 8
  • So basically, interfaces are a way to make behavior predictable across any class that implements them... It's just an organization technique? – rocksNwaves Feb 06 '20 at 18:31
  • yes. And because the interface only declares the method, you can use them. But the interface does not know anything about the class' internal structure. That's the reason to implement (define) them in the class – Jochen Feb 06 '20 at 18:36