Why would you ever use an interface if you are only going to have one implementation of it?
We need interfaces : To achieve total abstraction. To achieve security. Java doesn't allow multiple inheritance but it can be achieved by implementing multiples interfaces.
Provides communication − One of the uses of the interface is to provide communication. Through interface you can specify how you want the methods and fields of a particular type.
Interfaces are used in Java to achieve abstraction. By using the implements keyword, a java class can implement an interface. In general terms, an interface can be defined as a container that stores the signatures of the methods to be implemented in the code segment. It improves the levels of Abstraction.
Classes that almost never need an interface are: Classes that model some concept from your domain. Classes that otherwise represent stateful objects (as opposed to classes that represent stateless services).
If I knew for a fact that there would only ever be one implementation I wouldn't create an interface. This falls under YAGNI, IMO.
(Of course, it's rare that I know anything about the future for a fact...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With