-2

So, you create an interface and define some methods. Then if you implement the interface to a class you must override all of the methods of the interface. Then what is the purpose of an interface in this case, since you are just repeating yourself, rewriting methods?

  • 6
    You probably need to review the concept of abstraction in OOP. Will also help to read the solid principles. – ernest_k Nov 12 '21 at 07:13
  • 3
    And the [SOLID principles](https://en.wikipedia.org/wiki/SOLID), especially I and D. – Robby Cornelissen Nov 12 '21 at 07:15
  • 2
    Hint: you are expected to do serious research prior posting questions here. The internet, and also this community really has you covered. So, next time: try to use a search engine. It is a very safe bet that *any* basic question you can think of at this point has been asked and answered many times before, here and elsewhere. – GhostCat Nov 12 '21 at 07:23
  • Yes, there’s a lot to it. The answers give you some, not all of it. I love to use an interface when applying the façade design pattern, for example (you may look that up too if it’s not too advanced). – Ole V.V. Nov 12 '21 at 08:24

2 Answers2

1

To implement Polymorphism concept.

Deep
  • 153
  • 11
1

Java interfaces help you define rules.

For example, if you had a class for animal, you would want every class implementing animal have a method for movement, and number of limbs, the way they emit sounds etc.

You needn't know beforehand what those methods would be, but you do want to establish those set of rules that make an animal, an animal.

Kaizzen
  • 78
  • 7
  • But why do that, if you can separately define the methods, since you will be doing that regardless, when overriding them in each class… – Displayname Nov 12 '21 at 07:29
  • 2
    @Displayname But then it is pure *convention*. When you have that interface animal, and you write down `implements Animal` then the **compiler** will tell you if you miss to implement a method. Seriously, as written above: your question has been answered many times before. Learning programming is *very much* about learning how to find existing answers. Don't expect other people to explain you such super basic things ... that have been explained so many times already. – GhostCat Nov 12 '21 at 07:39
  • @Displayname adding to what GhostCat said, you want the compiler to let you know if you missed some. Every animal is bound by this contract of having to implement those methods. You will implement them anyway, but this will make sure you don't forget, and that the signatures don't change, adhering to consistent meaning. – Kaizzen Nov 12 '21 at 08:06
  • 1
    Thank you @Kaizzen, it makes a lot more sense now. I did do some research but please forgive me for how basic the question is. – Displayname Nov 13 '21 at 02:24