Are Interfaces in Java Just Abstract Methods?
Asked by
CodeCrafter92
I understand that interfaces and classes are fundamentally different in Java. However, at a very basic level, can we think of the methods defined in an interface as simply being abstract methods?
Answers
You're right; it does get a bit tricky. While traditionally, interfaces defined necessary functionality that implementing classes had to fulfill, the introduction of default methods means interfaces now can provide some functionality themselves. So yes, they still serve as contracts, but with new capabilities that blend functionality and abstraction.
Reply to this answer
Yes, at a fundamental level, you can view methods in an interface as abstract method signatures. They don't provide any implementation until a class decides to implement them. It used to be that these methods were purely abstract, but with Java 11, interfaces can also have non-abstract methods like default methods.
Reply to this answer
Interfaces are more about contracts than just methods. When you create an interface, you're not only defining method signatures but also what those methods are meant to achieve. For instance, while the List<T> interface specifies methods, the key point is how those methods should behave. If you implement this interface incorrectly, your list won't function as expected.
Reply to this answer
Add your answer
Answers are reviewed before publication.
CuriousCoder77: Also, you can't forget that classes can only extend one base class, so interfaces can help in achieving multiple inheritances.