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
A product from three cooperative sides
Read it, run it, change a line and run it again.
Loading editor…
Swap Priced and Labelled in the header and run again. There is no error. What is missing, and why?
The same in Java
import java.util.*;interface Labelled { String label(); default String key() { return label().toLowerCase(); }}interface Priced { int cents(); }interface Persistent { Map<String, Map<String, Object>> store(); String key(); Map<String, Object> export(); default void save() { store().put(key(), export()); }}record Product(String label, int cents, Map<String, Map<String, Object>> store) implements Persistent, Priced, Labelled { Product { if (cents < 0) throw new IllegalArgumentException("cents must not be negative"); } // Persistent.key() is abstract, Labelled.key() has a body: we must decide. @Override public String key() { return Labelled.super.key(); } @Override public Map<String, Object> export() { return Map.of("label", label, "cents", cents); }}class Main { public static void main(String[] args) { Map<String, Map<String, Object>> store = new HashMap<>(); new Product("Mug", 900, store).save(); System.out.println(store); }}The same in C++
#include <iostream>#include <map>#include <stdexcept>#include <string>using std::string;class Labelled {public: explicit Labelled(string label) : label_(std::move(label)) {} virtual ~Labelled() = default; const string& label() const { return label_; }private: string label_;};class Priced {public: explicit Priced(int cents) : cents_(cents) { if (cents < 0) throw std::invalid_argument("cents must not be negative"); } virtual ~Priced() = default; int cents() const { return cents_; }private: int cents_;};struct Persistent { // an interface with one finished step virtual ~Persistent() = default; virtual string key() const = 0; virtual string exported() const = 0; void save(std::map<string, string>& store) const { store[key()] = exported(); }};class Product : public Labelled, public Priced, public Persistent {public: Product(string label, int cents) : Labelled(std::move(label)), Priced(cents) {} string key() const override { return label(); } string exported() const override { return label() + " " + std::to_string(cents()); }};int main() { std::map<string, string> store; Product("mug", 900).save(store); std::cout << store["mug"] << '\n'; // mug 900}The same in TypeScript
type Ctor<T = object> = new (...args: any[]) => T;type Exported = Record<string, unknown>;class Entity { export(): Exported { return {}; }}function Labelled<B extends Ctor<Entity>>(Base: B) { return class extends Base { label = ""; key(): string { return this.label.toLowerCase(); } export(): Exported { return { ...super.export(), label: this.label }; } };}function Priced<B extends Ctor<Entity>>(Base: B) { return class extends Base { cents = 0; export(): Exported { return { ...super.export(), cents: this.cents }; } };}class Product extends Priced(Labelled(Entity)) { constructor(label: string, cents: number, private readonly store: Map<string, Exported>) { super(); if (cents < 0) throw new Error("cents must not be negative"); this.label = label; this.cents = cents; } save(): void { this.store.set(this.key(), this.export()); }}const store = new Map<string, Exported>();new Product("Mug", 900, store).save();console.log(store.get("mug")); // { label: 'Mug', cents: 900 }