Реализация интерфейса в разных подклассах с разными сущностями с использованием Java Generi c T extends - PullRequest
0 голосов
/ 14 июля 2020

Я пытаюсь достичь чего-то в Java с помощью Generics, не уверен, возможно это или нет. Если да, дайте мне знать правильный способ.

Это диаграмма UML, которая у меня есть. 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 {

    List 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 getAllUsers() {
   // SOME CODE
}

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

EmployeeManager methods should look like this:

@Override
public List 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?

введите описание изображения здесь

...