I have a method and as a parameter I send List. The method looks like this:
public static void setSanctionTypes(List<QueueSueDTO> items) {
for (QueueSueDTO dto : items) {
StringBuffer sb = sanctionTypeRutine(dto.getRegres().getDebtors());
String sanctionType = sb.toString();
dto.setSanctionType(sanctionType);
}
}
I need to use this method for different List data types parameters (for example setSanctionTypes(List<QueuePaymentDTO> items);
etc.).
All clases I want to send as a parameter have method getRegres()
, so content of setSanctionTypes()
method is common and usable for all these classes I want to send to it.
If I do this
public static void setSanctionTypes(List<?> items) {
for (Object dto : items) {
StringBuffer sb = sanctionTypeRutine(dto.getRegres().getDebtors());
String sanctionType = sb.toString();
dto.setSanctionType(sanctionType);
}
}
the dto of type Object doesn't know about getRegres(). I can cast to required type but it will be only one concrete type and it won't be usable for other parameters...
Is there way to resolve my problem ? Thanks.
You have do define an interface which forces the classes to implement getRegres(). Then you implement this interface for all classes you need and use: