3

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

11
  • What is your idea so far? Commented May 25, 2018 at 13:48
  • drive.google.com/open?id=1RhKAAS9ONkbzxZiZ4RCjxUaGomdOf18V Commented May 25, 2018 at 13:56
  • Please edit your question and include your approach instead of linking to clouds. Thank you! Commented May 25, 2018 at 13:58
  • I guess that A student can be a worker. A worker can also be a student. does not make sense. It implies mutual inheritance which is not possible. Commented May 25, 2018 at 14:24
  • It is not possible in Java, but in C++ it is… This is a question independent from a specific programming language, at least the tags just say uml, diagram and class-diagram. Commented May 25, 2018 at 14:32

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

But studentworker should be inheredit attribute and methods both from student and worker. In this solution studentworker is a third class indipendent from student and worker. Edit: in this case you do not respect the poliformism.
Basically you end up in the same answer as @Ouissal.
Basically you are right, but my answer directly anwers the question instead of giving general advice on how to design UML.
In Java language that is not possible because studentworker inheredit from two classes. Am i wrong?
You are right, it is not possible in Java. Why haven't you tagged your question with an additional Java-tag? In C++, you can do diamond inheritance.
1

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.

9 Comments

drive.google.com/open?id=1RhKAAS9ONkbzxZiZ4RCjxUaGomdOf18V This is my idea. But studenteoperaio (studentworker) does not take attributes "salary" and "school grades" (in italian salario and voti)
I can't see that file, I don't have read access. Student-worker will inherit from both classes student and worker, see this answer stackoverflow.com/questions/3541110/…
Now can you see the file?
@Ouissal Good answer, though more general than mine. You really know the difference between learning and writing off and you want the questioner to be a learner.
Thank you @deHaar
|
0

This may or may not be an answer to your exercise. If I got these requirements in the real world, I would refactor them. (Of course, in the real world, I would have a lot of questions about further refinements and details as well.) The real-world situation is (we may presume) that we are dealing with persons who always have a name and surname. Since the names and surnames don't change (we may also presume) when person is specialized into subclasses, there is no reason to provide a means to implement them differently in subclasses. Therefore there is no reason to make the person class abstract, since that's why you make abstract classes.

This is also a situation where aggregation (one class "has" other classes) makes more sense than inheritance (a subclass "is" a superclass). So I would restructure the requirements this way:

Person: Name, Surname Student: School, Grades Worker: Salary

Person class may "have" zero to one Student classes. Person class may "have" zero to one Worker classes.

There is also the implication, by requiring Person to be abstract, that a person must be either a student, a worker, or both. This can be specified by an or constraint between the two classes.

Here's a diagram:

enter image description here

Person class has an aggregate relationship with both student and worker classes. One person class may have zero to one student classes. One person class may also have zero to one worker classes. The {or} constraint further specifies that there must be one of either student or worker, or one of each.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.