Class representation in Venn diagram

1.4k views Asked by At

In our lesson our Teacher draw Class relation diagram with UML and Venn diagram.

Salaried Employee inherits Employee Class

Hourly Employee inherits Employee Class

(I do not really remember the context of classes)

He draw Venn diagram like this one :

Venn Diagram of Classes as for Teacher

But ,I really do not believe it should be like this. In my mind it is something like this :

Venn Diagram of Classes In my mind

By the way both of us assume classes private parts not included in Diagram representation ,or it would be like 3 set with intersections, for the simplification of diagram.

So my question is simple, which one is right/nearest to right representation.

4

There are 4 answers

5
Jason Baker On BEST ANSWER

In some sense they both are.

The first diagram accurately described the inheritance relation between the classes. That diagram is saying "In the set of all employees, some employees are hourly and some are salaried. There is no overlap between them." Employee is the more general set, so it contains the two more specific ones.

The second diagram is saying "There are employees that are both hourly and salaried. Some of those are employees." This obviously doesn't make a lot of sense in the context of inheritance.

However, the second diagram is a more-or-less accurate representation of what data is shared between those classes. The members of Hourly Employee and Salaried Employee objects will have some overlap, and that overlap is what is contained in Employee objects. For example, you might have some class definitions like this:

class Employee {
    int employee_id;
}

class SalariedEmployee extends Employee {
    int salary;
}

class HourlyEmployee extends Employee {
    int wage;
}

SalariedEmployee and HourlyEmployee both inherit the employee_id member from their superclass. If you were to draw the class definitions as a Venn diagram, the second diagram you've drawn is roughly what you'd come up with, but in a proper design the Employee class would be the entire intersection.

You may find it useful to think about inheritance relationships this way, especially as you're learning about OO, but in most contexts you'll find that the second diagram is typically implied rather than stated.

0
Thomas Matthews On

The first diagram is correct. Both salaried and hourly employees inherit from Employee. They both satisfy the "is-a" employee relationship.

2
Tri On

As per the statement, you mentioned that the assumption in your case is as follows: Salaried employees -- inherits---> Hourly employee -- inherits ---> Employee

If this is the case then I think both the diagrams are not correct.

It should be as follows:-

Biggest circle: Employee; Within that: Hourly; within that (smallest) : Employee

0
philipxy On

A Venn diagram puts cirlces around the elements of sets. Here each element is an employee. Imagine dots sprinkled on the page that are or are not enclosed in circles. Each dot is an employee. All the dots will fall inside the employee circle. Your teacher's diagram says hourly employees and salaried employees are employees, and that no employee is paid both by the hour and by salary.

One could also use the same setup to organize abstract C++ values representing/corresponding to employees.

You have to agree on the definitions of these terms/circles before you can draw them. Eg in some business maybe some employees are paid both ways; then the small cirlces overlap. Maybe there are no employees outside the small circles; maybe there are.

It's hard to know what you are trying to say about employees or what you think your teacher is trying to say unless you explain when a dot is supposed to fall in a circle.

(You may be thinking of the circle's elements as struct values, with circles including those that have a certain number of public members and/or fields or fewer. Or that circle elements are public struct members and/or fields of classes corresponding to various kinds of employees.

But then your employee circle should overlap both kinds of employee circle.)