Multiple inheritance
hard 50 minA class with several parent classes gets the types, the methods and the fields of all of them.
Watch lesson4:37
Problem
Two sides of one person.
The point: before you name two parents, list what comes from each: type, behaviour, state, and the names they share.
A college system has persons, students and employees. Some students work. Reports need students, payroll needs employees, and the canteen needs persons. The same human must not appear twice.
Inventory
What the join would receive
| Member | From Student | From Employee | Verdict |
|---|---|---|---|
name | through Person | through Person | One copy only, or the two will differ |
school, company | school | company | No clash |
study(), work() | study | work | No clash |
describe() | its share | its share | Same promise: cooperate |
| Constructor | name, school | name, company | Must agree on one way |
id() (asked for later) | student number | staff number | Same word, two promises: rename |
Ask
Questions that decide the design
- 1
Does a side end?
If students graduate and the object must live on, roles are better than parents. In this task objects are made new each term, so it does not.
- 2
How many sides will come?
Two sides make one join class. Five sides make up to 26 of them.
- 3
Who owns the parents?
Ours. So we may make them cooperative. A parent from a library is another case.
- 4
Which languages?
Python and C++ have the feature. Java and TypeScript need the same design with other tools.