Should I return List or ArrayList?

arraylist, interface, java

Solution

For types like List or ArrayList there shouldn't be any complication and you should return List promoting code to an interface.

You will find yourself restricted doing so, had this been from the Concurrency package like CopyOnWriteArrayList and you were using methods like addIfAbsent which is not defined in the List interface.

So, if you return ArrayList or any concrete implementation for that matter you can use API which are not defined in the contract (`List` interface), but then you are restricted in changing from a specific implementation to something else (from `ArrayList` to `LinkedList`), as everyone using your API will have to change as per your changes. It's too much to expect I think.

Problem

I find myself agreeing to return an interface instead of a concrete class. The reason is simple, I want loose coupling. But will there be other implications or trade offs?

Original source