AlgoViz

IS-A and HAS-A

easy 40 min

IS-A says that an object is a kind of something and can stand in for it.

Watch lesson4:45

A phone that has a battery

Read it, run it, change a line and run it again.

Loading editor…

The last lines are a test with a fake part. Nothing had to be inherited for it.

The same in Java
interface Battery {    int capacity();    String charge();}record StandardBattery() implements Battery {    public int capacity() { return 3000; }    public String charge() { return "standard charging"; }}record FastChargeBattery() implements Battery {    public int capacity() { return 5000; }    public String charge() { return "fast charging"; }}final class Phone {    private final String brand;    private Battery battery;    Phone(String brand, Battery battery) { this.brand = brand; this.battery = battery; }    String charge() { return battery.charge(); }    Battery swapBattery(Battery next) {        Battery old = battery;        battery = next;        return old;    }    String info() { return brand + ", " + battery.capacity() + " mAh"; }}public class Main {    public static void main(String[] args) {        Phone phone = new Phone("FlexPhone", new StandardBattery());        System.out.println(phone.info() + " | " + phone.charge());        phone.swapBattery(new FastChargeBattery());        System.out.println(phone.info() + " | " + phone.charge());    }}
The same in C++
#include <iostream>#include <memory>#include <string>#include <utility>class Battery {public:    virtual ~Battery() = default;    virtual int capacity() const = 0;    virtual std::string charge() const = 0;};class StandardBattery : public Battery {public:    int capacity() const override { return 3000; }    std::string charge() const override { return "standard charging"; }};class FastChargeBattery : public Battery {public:    int capacity() const override { return 5000; }    std::string charge() const override { return "fast charging"; }};class Phone {    std::string brand_;    std::unique_ptr<Battery> battery_;public:    Phone(std::string brand, std::unique_ptr<Battery> battery)        : brand_(std::move(brand)), battery_(std::move(battery)) {}    std::string charge() const { return battery_->charge(); }    std::unique_ptr<Battery> swapBattery(std::unique_ptr<Battery> next) {        std::swap(battery_, next);        return next;    }    std::string info() const { return brand_ + ", " + std::to_string(battery_->capacity()) + " mAh"; }};int main() {    Phone phone("FlexPhone", std::make_unique<StandardBattery>());    std::cout << phone.info() << " | " << phone.charge() << "\n";    phone.swapBattery(std::make_unique<FastChargeBattery>());    std::cout << phone.info() << " | " << phone.charge() << "\n";}
The same in TypeScript
interface Battery {  readonly capacity: number;  charge(): string;}class StandardBattery implements Battery {  readonly capacity = 3000;  charge(): string { return "standard charging"; }}class FastChargeBattery implements Battery {  readonly capacity = 5000;  charge(): string { return "fast charging"; }}class Phone {  constructor(readonly brand: string, private battery: Battery) {}  charge(): string { return this.battery.charge(); }  swapBattery(next: Battery): Battery {    const old = this.battery;    this.battery = next;    return old;  }  info(): string { return this.brand + ", " + this.battery.capacity + " mAh"; }}const phone = new Phone("FlexPhone", new StandardBattery());console.log(phone.info(), "|", phone.charge());phone.swapBattery(new FastChargeBattery());console.log(phone.info(), "|", phone.charge());