0

All the methods declared in interfaces are abstract and we have to re-write the method with signature and body in the class that implements the interface. So what's the point of using the interface?

subash poudel
  • 37
  • 1
  • 7
  • 1
    Does this answer your question? [Interface vs Abstract Class (general OO)](https://stackoverflow.com/questions/761194/interface-vs-abstract-class-general-oo) – ThisIsNoZaku Apr 09 '20 at 05:55
  • @ThisIsNoZaku OP is asking for _why_ not what the difference is – awarrier99 Apr 09 '20 at 05:56
  • We need interfaces to **declare** abstract methods, exactly as you state. Then we **implement** those methods in a concrete class. That's the point. This is all about the concept of abstraction. Or are you asking why we use abstraction in programming? – ernest_k Apr 09 '20 at 05:56
  • 1
    https://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface?rq=1 – Mat Apr 09 '20 at 05:59
  • @awarrier99 I retracted my flag but the comment remained. – ThisIsNoZaku Apr 09 '20 at 05:59
  • The premise of your question (*"All the methods declared in interfaces are abstract"*) is not even necessarily true since the interface may have default methods. See [Interface with default methods vs Abstract class in Java 8](https://stackoverflow.com/q/19998454/2985643). Can you edit your question accordingly? – skomisa Apr 09 '20 at 06:43

2 Answers2

4

Generally interfaces are considered to be contracts between developers. By this I mean, let's say you are developing a proprietary API for public use. Now you don't want everyone to be able to directly see your source code of how you implemented something, because that is your whole product. What you do instead is expose an interface which shows other developers what parameter types and return values to expect, and guarantees that your code will accomplish the purpose that it is being used for, without having to reveal how it works.

This also allows for the extensibility of code, because by allowing a certain method to accept an interface, rather than a specific implementation, you then allow the implementation to be changed or possibly improved, as long as it still implements the same interface and therefore adheres to the same guarantees.

Defining an interface is like saying "I expect to be able to have these functions that I can call that accept these parameters and return these values, but I don't care how you do it"

awarrier99
  • 3,628
  • 1
  • 12
  • 19
0

Interface just like the name suggest provides interface to something. So let's say I want an application that takes input from user and stores it in a database and later on fetches data from database on request to display from user. I can simply have an interface that declares methods to store and fetch from the database. So the user form that takes input and displays data can use these methods. This gives me flexibility to change the implementation if I decide to change the database. And I would not have to change the part using the methods declared in the interface. Since all implementation will have those methods.

I think this doc(https://docs.oracle.com/javase/tutorial/java/concepts/interface.html) also will be helpful.

I think I can add one more thing here, let's say I want to use the interface provided by facebook(or some other app if you like). I can go through the list of methods provided to see which one works for me. It makes things simpler than having to go through all the implementation details. That's what I think @Tarun also said in above answer.

Bin
  • 1
  • 2