Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Useless interfaces

Tags:

Why would you ever use an interface if you are only going to have one implementation of it?

like image 224
Hubert Avatar asked Jun 05 '09 14:06

Hubert


People also ask

Do we really need interfaces?

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.

Why are interfaces needed?

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.

Where are interfaces used?

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.

Should all classes have interface?

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).


1 Answers

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...)

like image 120
Bill the Lizard Avatar answered Oct 06 '22 17:10

Bill the Lizard