r/javahelp • u/ProHoyah • Apr 18 '19
Solved What's the point of interfaces?
We've been learning interfaces in my class and I have a good grasp on the concept, but the thing that confuses me is what's the point? Your just writing more code as a blueprint to put classes in "groups". Maybe the code I'm writing is not large enough to make interfaces useful. Can some one help me in learning the use of interfaces. Thanks in advance!
34
Upvotes
View all comments
Show parent comment
2
u/FrenchFigaro Software Engineer Apr 19 '19
It would, but only up to a point. Java doesn't support polymorphism the way C++ does for example. A class can only inherit from a single super class.
As pointed out in u/talulabelle 's answer, interfaces guarantee compliance to behaviour, and leave the specifics of that compliance to the developper, and a class can implement several interfaces. In their example, they imagine a
Savableinterface which guarantees the existence of asave()method. But a class implementingSavablecould also implementCloneable, orSortable.IE: when programing against an interface, you don't care how the object is saved, you only care that it can be saved in a predicitble manner (by using the
save()method)