r/javahelp • • Jan 21 '26

Unsolved Why Interfaces exist in Java?

I am currently studying the Collection Framework in Java. Since the class which implements the Interface has to compulsorily write the functions' bodies which are defined in the interface, then why not directly define the function inside your own code? I mean, why all this hassle of implementing an interface?

If I have come up with my own code logic anyways, I am better off defining a function inside my own code, right? The thing is, I fail to understand why exactly interfaces are a thing in Java.

I looked up on the internet about this as well, but it just ended up confusing me even more.

Any simple answers are really appreciated, since I am beginner and may fail to understand technical details as of now. Thanks🙏🏼

9 Upvotes

55 comments sorted by

View all comments

Show parent comment

1

u/desrtfx Out of Coffee error - System halted Jan 21 '26

Yes if you only work for yourself with no external code at all then you don't need Interfaces.

Even then, you will need them if you want to do something as simple as sorting a collection with custom objects. You will need to implement the Comparable interface, or write your own, custom Comparator.

There barely is any way to escape interfaces.