Inheritance

Demonstrates inheritance and method overriding; Go expresses a similar effect with struct embedding and shadowing.

C C23

C23
#include <stdio.h>

// inheritance idiom: the subtype embeds the base type at its head + per-type vtables implement overriding and dynamic dispatch
typedef struct {
    const char *(*speak)(void);
} AnimalVTable;

typedef struct {
    const AnimalVTable *vtable;
} Animal;

static const char *animal_speak(void) {
    return "Animal speaks";
}

static const AnimalVTable animal_vtable = { animal_speak };

// Dog "inherits" Animal: the base part is at the head of the sub-struct (upcastable to Animal*)
typedef struct {
    Animal base;
} Dog;

static const char *dog_speak(void) { // override
    return "Woof";
}

static const AnimalVTable dog_vtable = { dog_speak };

int main(void) {
    Dog d = { { &dog_vtable } };
    printf("%s", d.base.vtable->speak()); // dynamic dispatch: calls Dog's implementation
    return 0;
}
Output
Woof

C++ C++26

C++26
#include <iostream>
#include <string>
class Animal {
public:
    virtual std::string speak() const { return "Animal speaks"; } // virtual functions allow overriding
    virtual ~Animal() = default;
};
class Dog : public Animal {
public:
    std::string speak() const override { return "Woof"; } // override marks it explicitly
};
int main() {
    Dog d;
    std::cout << d.speak() << "\n";
    return 0;
}
Output
Woof

C# v11.0.100

v11.0.100
var d = new Dog();
Console.WriteLine(d.Speak());

class Animal {
    public virtual string Speak() => "Animal speaks"; // virtual method: overriding allowed
}
class Dog : Animal {
    public override string Speak() => "Woof";
}
Output
Woof

Go v1.27.4

v1.27.4
package main

import "fmt"

type Animal struct{}

func (a Animal) Speak() string {
    return "Animal speaks"
}

type Dog struct {
    Animal
}

func (d Dog) Speak() string {
    return "Woof"
}

func main() {
    d := Dog{}
    fmt.Println(d.Speak())
    fmt.Println(d.Animal.Speak())
}
Output
Woof
Animal speaks

Java v27.0.1

v27.0.1
public class Main {
    static class Animal {
        String speak() {
            return "Animal speaks";
        }
    }
    static class Dog extends Animal {
        @Override
        String speak() {
            return "Woof";
        }
    }
    public static void main(String[] args) {
        Dog d = new Dog();
        System.out.println(d.speak());
    }
}
Output
Woof

JavaScript v26.13.1

v26.13.1
class Animal {
    speak() {
        return "Animal speaks";
    }
}
class Dog extends Animal {
    speak() {
        return "Woof";
    }
}
const d = new Dog();
console.log(d.speak());
Output
Woof

PHP v8.6.0

v8.6.0
class Animal {
    public function speak(): string {
        return "Animal speaks";
    }
}
class Dog extends Animal {
    public function speak(): string {
        return "Woof";
    }
}
$d = new Dog();
echo $d->speak();
Output
Woof

Python v3.15.2

v3.15.2
class Animal:
    def speak(self):
        return "Animal speaks"

class Dog(Animal):
    def speak(self):
        return "Woof"

d = Dog()
print(d.speak())
Output
Woof

Rust v1.99.1

v1.99.1
trait Speak {
    fn speak(&self) -> String {
        "Animal speaks".to_string()
    }
}

struct Dog;

impl Speak for Dog {
    fn speak(&self) -> String {
        "Woof".to_string()
    }
}

fn main() {
    let d = Dog;
    println!("{}", d.speak());
}
Output
Woof

TypeScript v26.13.1

v26.13.1
class Animal {
    speak(): string {
        return "Animal speaks";
    }
}
class Dog extends Animal {
    override speak(): string {
        return "Woof";
    }
}
const d = new Dog();
console.log(d.speak());
Output
Woof

Language differences

C: no classes — the example uses the struct + function-pointer vtable idiom (OOP-in-C), with semantics enforced by convention.

Go: no inheritance — embedding Animal in Dog promotes its methods, and a same-named Speak shadows the promoted one (the example calls the embedded type method explicitly via d.Animal.Speak()), which is not OO overriding with dynamic dispatch.

Java: uses extends (single inheritance plus multiple interfaces); parent methods via super.

JavaScript: uses extends; parent methods via super.

PHP: uses extends; parent methods via parent::.

Python: supports multiple inheritance and calls parent methods with super().

Rust: no inheritance — polymorphism with traits plus default methods.