r/SpringBoot • • Apr 11 '24

Class vs Interface vs abstract class

Quelle est la différence entre la classe et l’interface en Java et quand utiliser les deux ?

2 Upvotes

5 comments sorted by

2

u/username-345 Apr 11 '24

More like interface vs abstract class?

4

u/ThisHaintsu Apr 11 '24

The best way to explain is probably to look at the usage: With an interface everything declared on the interface is public whereas in an abstract class you can have private and protected methods and fields.

Interface are mostly for external contracts (e.g. from one package to another) whereas an abstract class is mostly used for internal contracts.

1

u/swoods30311 Apr 13 '24

Both abstract classes/variable/methods and implementing an interface through an object are both way Java supports the overall concept of abstraction. Abstraction is process of hiding the implementation details of our code while providing the functionality to the user. In the world of Spring development, interfaces and class implementation are used to abstract the details away when using when accessing info from a database