I have the interface defined as below and is implemented by a single class MatchedAddressImpl.
interface MatchedAddress extends HouseHelpData, StreetHelpData, TownHelpData
public class MatchedAddressDetails implements MatchedAddress
the client should be provided different views (HouseHelpData or StreetHelpData or TownHelpData or MatchedAddress) of the same MatchedAddressImpl. So I have provided the below API for the clients.
public List<MatchedAddress> matchedAddresses()
public List<? extends HouseHelpData> houseHelpData()
public List<? extends StreetHelpData> streetHelpData();
public List<TownHelpData> townHelpData();
the problem is that the client needs to do something like below and I read ineffective java that the return types should not contain wild cards as the client usage looks ugly...I appreciate it if someone can help me improve the API. what I want is to remove the wildcards from the above methods.
List<? extends StreetHelpData> streetHelpDataList = details.streetHelpData();
For most cases, the proper usage is simply List<StreetHelpData>. You would still be able to put objects of type StreetHelpDataImpl, for instance.
Wildcards are, in my opinion, misleading. Basically, for List<? extends StreetHelpData> it would mean: "This list contains elements all of a certain type, which is a sub-type of StreetHelpData."
Wildcard example:
Consider :
class Animal {}
class Lion extends Animal {}
class Tiger extends Animal {}
The list List<? extends Animal> contains either Lions only (List<Lion>), Tigers only (List<Tiger>), or both (List<Animal>). However, the list List<Animal> can contain all sorts of Animals - Lions and/or Tigers - at all times.
(Thanks to Tom Hawtin for his pointers)
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