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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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 (
Listinterface), but then you are restricted in changing from a specific implementation to something else (fromArrayListtoLinkedList), as everyone using your API will have to change as per your changes. It’s too much to expect I think.