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

Sphinx
  • 956
  • 7
  • 21
Alberto Morreale
  • 435
  • 1
  • 4
  • 11
  • What is your idea so far? – deHaar May 25 '18 at 13:48
  • https://drive.google.com/open?id=1RhKAAS9ONkbzxZiZ4RCjxUaGomdOf18V – Alberto Morreale May 25 '18 at 13:56
  • Please edit your question and include your approach instead of linking to clouds. Thank you! – deHaar May 25 '18 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. – qwerty_so May 25 '18 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. – deHaar May 25 '18 at 14:32
  • In the title i wrote "java" – Alberto Morreale May 25 '18 at 14:33
  • OK, I have seen that, will edit my answer soon – deHaar May 28 '18 at 06:38
  • @deHaar I don't believe in C++ (correct me if I'm wrong) that you can have mutual inheritance, where A is both a superclass and subclass of B, and vice versa. I believe that you are confusing that with multiple inheritance, which is of course possible in C++ and is, as you say, language-specific. – BobRodes Jun 05 '18 at 03:16
  • @BobRodes Sorry for not being clear enough. You are right, of course... Mutual inheritance is not possible in any of the programming languages I know (OK, not that many, admittedly). My comment was about multiple inheritance (available in C++, not available in Java, not really sure about C#). – deHaar Jun 05 '18 at 06:03
  • I'm surprised nobody mentioned the usage of the party-partyrole pattern. A person **is** not a worker, he plays the **role** of a worker, and the **role** of a student. Student and person are then subtypes of PartyRole, and a person is a subtype of Party. – Geert Bellekens Jun 05 '18 at 07:48
  • @deHaar Not available in C# either, FYI. If I were more cynical than I am, I would say that C# was yet another attempt by Microsoft to create a proprietary version of an open standard, in this case Java. :) – BobRodes Jun 06 '18 at 04:40

3 Answers3

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.

deHaar
  • 17,687
  • 10
  • 38
  • 51
  • 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. – Alberto Morreale May 25 '18 at 14:13
  • Basically you end up in the same answer as @Ouissal. – qwerty_so May 25 '18 at 14:26
  • 1
    Basically you are right, but my answer directly anwers the question instead of giving general advice on how to design UML. – deHaar May 25 '18 at 14:27
  • In Java language that is not possible because studentworker inheredit from two classes. Am i wrong? – Alberto Morreale May 25 '18 at 14:28
  • 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. – deHaar May 25 '18 at 14:35
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.

Ouissal
  • 1,519
  • 2
  • 18
  • 36
  • https://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) – Alberto Morreale May 25 '18 at 13:53
  • 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 https://stackoverflow.com/questions/3541110/c-multiple-inheritance-with-polymorphism – Ouissal May 25 '18 at 14:02
  • Now can you see the file? – Alberto Morreale May 25 '18 at 14:08
  • @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. – deHaar May 25 '18 at 14:10
  • Thank you @deHaar – Ouissal May 25 '18 at 14:17
  • @AlbertoMorreale No I can't see it, but I've edited my answer, I guess that's what you've meant – Ouissal May 25 '18 at 14:18
  • But D (StudentWorker) it is a class that extends two classes (student, worker) and this is not possibile in java. I thought to use interfaces but i manage to arrive to the solution. – Alberto Morreale May 25 '18 at 14:24
  • Yes Java doesn't support multiple inheritance, if you're using Java then you can use interfaces instead, see this example : https://www.geeksforgeeks.org/java-and-multiple-inheritance/ – Ouissal May 25 '18 at 14:27
  • Multiple inheritance is evil per se and I'd never model anything in that direction. The requirement as it's written is plain nonsense and needs to be interrogated. A recursive inheritance (A inh. B and B inh. A) is wrong. So your solution is technically correct, but from a reality view it's plain nonsense. – qwerty_so May 25 '18 at 14:30
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.

BobRodes
  • 5,990
  • 2
  • 24
  • 26