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

One word, two promises

A copier that starts.

The point: overriding once overrides both. If the promises differ, one caller gets the wrong behaviour without any error.

A copier prints and scans. The printer library has start() for printing, the scanner library has start() for scanning. A night job scans the glass of every scanner.

Two sides instead of two parents

«abstract»Printer+ start()+ printJob()«abstract»Scanner+ start()+ scanTarget()NightBackup+ run(scanner)

1/4Two libraries

Both have start(). We can change neither. The night job knows scanners only.

extends (is a)usesowns (dies together)

From the clash to two sides

Each is fine alone. They share one word.

from abc import ABC, abstractmethodclass Printer(ABC):    def start(self) -> str:        """Takes paper and prints the pages that wait."""        return f"printing: {self.print_job()}"    @abstractmethod    def print_job(self) -> str: ...class Scanner(ABC):    def start(self) -> str:        """Moves the lamp and reads the glass."""        return f"scanning: {self.scan_target()}"    @abstractmethod    def scan_target(self) -> str: ...

Lines 4 to 7

The promise is in the text, not in the signature. No compiler can read it.

1 of 2

How languages separate them

LanguageTool
C++Two middle classes that forward to two new names, then the join
C#Explicit implementation: string IScanner.Start() next to string IPrinter.Start()
Eiffelinherit LIST rename ...: the tutorial's ARRAYED_LIST takes LIST and ARRAY and renames count to capacity
RustTwo traits; the caller writes Scanner::start(&copier)
Java, Python, TypeScriptTwo objects