AlgoViz

Multiple inheritance

hard 50 min

A 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

MemberFrom StudentFrom EmployeeVerdict
namethrough Personthrough PersonOne copy only, or the two will differ
school, companyschoolcompanyNo clash
study(), work()studyworkNo clash
describe()its shareits shareSame promise: cooperate
Constructorname, schoolname, companyMust agree on one way
id() (asked for later)student numberstaff numberSame word, two promises: rename

Ask

Questions that decide the design

  1. 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. 2

    How many sides will come?

    Two sides make one join class. Five sides make up to 26 of them.

  3. 3

    Who owns the parents?

    Ours. So we may make them cooperative. A parent from a library is another case.

  4. 4

    Which languages?

    Python and C++ have the feature. Java and TypeScript need the same design with other tools.