views:

200

answers:

6

Should interfaces in Java reside in their own directory? Or should both the interface and its implementation be placed in the same directory (package)? Thanks.

A: 

Same package. The user should not know or care that they are using an interface

Kevin
Yes. Use of `I` prefix is nuts.
Tom Hawtin - tackline
The user needs to know if they have to instantiate these classes themselves.
R. Bemrose
"Same package" doesn't answer the question. It's very common to have a unit test in the same package as the class it is testing, yet having it in a completely different directory.
Webinator
+3  A: 

Interfaces don't specifically need their own directory. They should be placed where it makes sense, just as classes should be placed where they make sense. In many cases, it may make sense to put them in the same place.

Aaron
+2  A: 

One pattern I've seen is to put the interfaces in a base directory, then put the implementations in a subdirectory from there.

For example, interfaces might go here:

com.myproject.data.dao.CustomerDao (some people do ICustomerDao for interfaces, but some don't like that.)
com.myproject.data.dao.ProductDao

And the implementations might go here:

com.myproject.data.dao.hibernate.HibernateCustomerDao
com.myproject.data.dao.hibernate.HibernateProductDao
com.myproject.data.dao.someotherorm.SomeOtherOrmCustomerDao
etc.

This might work in some situations, and might not in others, but just something to think about.

Andy White
thanks for this perspective
ken
A: 

Its Not at all necessary to place the interface in the same directory(package). If your interface has a public access, then you can import it anywhere, in any package.

Mohit
A: 

The question as I read it (but then it's strangely formulated) is not if interface should be in their own directory or not. The question is if you should recreate your complete directory structure (bold to emphasis what is in the question's title) where one branch would contain only interfaces, like this:

pureooabstraction/
 |
 |_com/
   |
   |_example/
     |
     |__SomeInterface.java
     |__SomeOtherInterface.java

src/
 |
 |_com/
   |
   |_example/
     |
     |__SomeClass.java
     |__...

Where the pureooabstraction/ directory structure would contain only "pure abstract classes" (from an OO point of view, not the Java 'abstract' definition), aka interfaces in Java.

And the petty implementation details (which don't exist at the OOA/OOD level) where "code" lies would go in the src/ directory.

It certainly makes sense if your development process goes from OOA to OOD to OOP.

Webinator
no, he was just asking whether to put the interfaces in the same package as the implementations
Ken Liu
+1  A: 

As there are already some good points, I just wanna add one thing:

In some projecs we have even gone so far that we placed all interfaces into one sub-project (maven module) and implementations into another one. This way it was possible to FULLY seperate the interfaces from the implementations and finalize the interface project very early in the project and deliver it to other teams working agains those interfaces. Within each of the projects we used the same packages.

In general I would say, you should seperate interfaces and their implemetations, the way doesnt really matter, as long as you are consistent with it.

Nils Schmidt