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.
The INTERFACE structure defines a structured block of related members that may be passed as a single parameter to complex queries, instead of passing each attribute individually. It is similar to a MODULE structure with the VIRTUAL option, except errors are given for private (not SHARED or EXPORTed) member definitions.
You should put it in a separate file. That way it's easier to swap in a different implementation, or make the interfaces (the API of your system) available for others to code against without knowing the details of your implementation, or having to drag in related dependencies.
Here's a suggestion, if almost all of your interfaces are to support only one class, just add the interface to the same file as the class itself under the same namespace. That way you don't have a separate file for the interface which could really clutter the project or need a sub folder just for interfaces.
To declare an interface, use the interface keyword. It is used to provide total abstraction. That means all the methods in an interface are declared with an empty body and are public and all fields are public, static, and final by default.
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.
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.
As there are already some good points, I just wanna add one thing:
In some projects 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.
Same package. The user should not know or care that they are using an interface
Wherever you want, but it is absolutely fine to keep interfaces in the same package and directory structure. Just look at the java api. If you select any of the packages you will notice that many contain both classes and interfaces. Some of the interfaces are implemented by classes in the same package, and some are not.
I think the worst practice is to insist that you must have a different directory for interfaces. I have seen directories like /services and /impl and others, which only muck up the directory structure. At my current workplace we employ a lot of contractors that come and go, and some of our projects have multiple types of interface directories. The only time I think it makes sense to use a separate directory is if you plan to copy the interfaces into other projects, say for EJBs, but even then they can have the same package if you use a shared project for the interfaces.
So short answer is, anywhere you want, but don't think you need to separate your classes and your interfaces. In many cases it is preferable to keep them in the same package/directory.
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