Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java UML Student Worker project multiple inheritance

There's an exercise I have which requires us to provide a UML Diagram for the following:

  • A Person class (Abstract)
  • Student (name, surname, school, grades)
  • Worker (name, surname, salary) extends Person

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:

enter image description here

like image 930
Alberto Morreale Avatar asked Nov 28 '25 19:11

Alberto Morreale


2 Answers

A possible solution would be the following: enter image description here

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.

enter image description here

Second Update

This solution only makes use of implementing interfaces:

enter image description here

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.

like image 53
deHaar Avatar answered Dec 01 '25 23:12

deHaar


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 :

enter image description here

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 :

enter image description here

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.

like image 43
Ouissal Benameur Avatar answered Dec 02 '25 00:12

Ouissal Benameur



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!