Reputation: 785
Say there is a class called Thing
.
Would it be a bad practice to have an interface return a Thing?
interface Doable{
public Thing generateThing();
}
What if Thing wasn't a class, but just another interface (Type). Would this be a bad practice?
Thanks.
Upvotes: 0
Views: 177
Reputation: 580
I think it's not a bad practice, but you have better to consider the context. You should understand which is the expected implementation and ask yourself if the code will be easily reusable
Upvotes: 0
Reputation: 2373
As a rule you return the most general type that makes sense for the use case. For instance, imagine if you wanted to return a collection of things:
Collection
is usually too general, people using the collection usually need to know what kind of collection it is.
ArrayList
on the other hand is most often too specific, users of your interface don't usually care how your list stores it's values internally.
List
then is perfect. Which is an interface.
So no. Why would it be a bad practice.
Upvotes: 3