There's an exercise I have which requires us to provide a UML Diagram for the following:
A student can be a worker. A worker can also be a student. How would I achieve this?
This is my solution, but I understand that it's not efficient:

A possible solution would be the following:

This solution adds an extra class for the case of a student being a worker the same time.
First Update
The following image shows how StudentWorker may inherit from Worker and Student, though this might not be possible in every programming language and might as well lead to the Diamond Problem.

Second Update
This solution only makes use of implementing interfaces:

Please note that you still need 3 classes. Student and Worker implement two interfaces each, the StudentWorker implements all three interfaces. That makes all three classes a Person as well as a Student an IStudent, a Worker an IWorker and a StudentWorker an IStudent and an IWorker. I hope this helps or gives you an idea on how to create your individual solution.
Student and worker will both be a subclass of the class Person. Person will have the common attributes which are common (name and surname). Student will have school grades and worker will have salary.
This is how you can represent inheritance in UML :

You have a superclass, and your derived classes. Since your superclass is abstract, it will have methods that are declared without an implementation, you'll implement them in your classes Student and Worker (if you have to).
If you have another class student-worker, it will inherit from both student and worker. So it will be something like this :

A is your abstract class Person, B and C are Student and Worker respectively, while D is StudentWorker. StudentWorker will inherit the attributes of both Student and Worker.
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