1

I am trying to achieve something in Java using Generics, not sure if this is possible or not. If yes, please let me know the correct way.

This is the UML Diagram I have. enter image description here

User is the base entity and Customer and Employee are both its subclass (or extending the User Class).

Now there is an interface called UserManager, and CustomerManager and EmployeeManager are both extending the interface. This is how my UserManager interface looks like:

public interface UserManager<T extends User> {

    List<T> getAllUsers();

    void createUser(T user);
}

What I am trying to achieve is both the implementing class CustomerManager and EmployeeManager should implement the methods but they should use their respective Entity instead of using the generic T in their implementations. Like:

CustomerManager methods should look like this:

@Override
public List<Customer> getAllUsers() {
   // SOME CODE
}

@Override
public void createUser(Customer user) {
   // SOME SODE
}

EmployeeManager methods should look like this:

@Override
public List<Employee> getAllUsers() {
   // SOME CODE
}
    
@Override
public void createUser(Employee user) {
   // SOME SODE
}

But I am not able to achieve this, it is working for the getAllUsers method but not for the createUser method, as you can see in the screenshot below. Can anyone help me identifying the mistake I am making here?

enter image description here

Rito
  • 3,092
  • 2
  • 27
  • 40

0 Answers0