I have 2 classes
public class Customer{
...
public String getCustomerNumber();
...
}
public class Applicant{
....
private Customer c;
public Customer getCustomer(){ return c; }
...
}
When presented with a list of customers or applicants I want a function which iterates the list and does something with the CustomerNumber.
I've tried overloading the function
public void processCustomerNumbers(List<Customer> custList)
...
public void processCustomerNumbers(List<Applicant> appList)
...
but these are seen as duplicate methods... is there a nice way of doing this rather than just having 2 differently named functions?
No, you cannot overload a method based on different return type but same argument type and number in java. same name. different parameters (different type or, different number or both).
Parameter Order: If the two methods have the same number of parameters and the same type of parameters but if the order of parameters is different, overloading works.
With respect to the method it overrides, the overriding method must follow following mandatory rules: It must have the same method name. It must have the same arguments. It must have the same return type.
The argument list should be exactly the same as that of the overridden method. The return type should be the same or a subtype of the return type declared in the original overridden method in the superclass.
If you make both classes implement a common interface,
interface CustomerNumber {
String getCustomerNumber();
}
public class Customer implements CustomerNumber {
...
public String getCustomerNumber();
...
}
public class Applicant implements CustomerNumber {
....
private Customer c;
public Customer getCustomer() { return c; }
public String getCustomerNumber() { return getCustomer().getCustomerNumber(); }
...
}
then you might be able to do what you want with just a single method:
public void processCustomerNumbers(List<? extends CustomerNumber> appList) {
for (Customer c: appList) {
processCustomerNumber(c.getCustomerNumber());
}
}
The thing about generics in Java is that generic types are erased at runtime, so both of these methods compile to the same signature. You will need to have separate method names, or check the type of the list elements at runtime.
One way to workaround this issue would be to define custom list types like this:
class CustomerList extends ArrayList<Customer> {
...
}
class ApplicantList extends ArrayList<Applicant> {
...
}
Then the following overloading would be legal:
public void processCustomerNumbers(CustomerList custList)
public void processCustomerNumbers(ApplicantList appList)
However, I don't think that this would be a good idea. For a start, it hardwires particular implementation classes into your application's APIs.
A better approach is to define a common interface for Customer and Applicant that allows you to process them with one processCustomerNumbers
method. (As described at length in other answers.)
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