Class Inheritance — Clean Language
← All tutorials
language-deep-dive7 min

Class Inheritance

Inheritance lets a class build on another. The child gets all the parent's properties and methods automatically, can add its own, and can call the parent's constructor with base(). Use it when two things genuinely share structure — not just to reuse code.

A parent class and a child that extends it:

class Animal\n    string name\n    string sound\n\n    constructor(string n, string s)\n        name = n\n        sound = s\n\n    string speak()\n        return name + " says " + sound + "!"\n\nclass Dog is Animal\n    string breed\n\n    constructor(string n, string b)\n        base(n, "Woof")\n        breed = b\n\n    string describe()\n        return name + " is a " + breed\n\nstart:\n    Animal cat = Animal("Whiskers", "Meow")\n    Dog dog = Dog("Rex", "Labrador")\n\n    print(cat.speak())\n    print(dog.speak())\n    print(dog.describe())
Whiskers says Meow!\nRex says Woof!\nRex is a Labrador

Dog inherits from Animal with class Dog is Animal. base(n, "Woof") calls Animal's constructor with Rex's name and a hardcoded sound. Dog automatically inherits speak() and accesses the name property — no re-declaration needed. Dog adds its own breed property and describe() method.

A child can override a parent method by defining its own version with the same name:

class Shape\n    string color\n\n    constructor(string c)\n        color = c\n\n    string info()\n        return "A " + color + " shape"\n\nclass Circle is Shape\n    number radius\n\n    constructor(string c, number r)\n        base(c)\n        radius = r\n\n    number area()\n        return 3.14159 * radius * radius\n\n    string info()\n        return "A " + color + " circle, radius " + radius.toString() + ", area " + area().toString()\n\nstart:\n    Shape s = Shape("grey")\n    Circle c = Circle("red", 5.0)\n\n    print(s.info())\n    print(c.info())
A grey shape\nA red circle, radius 5.0, area 78.53975

Circle defines its own info() method, which replaces the parent's version for Circle instances. The color property from Shape is accessible in Circle without re-declaration. This is method overriding — the child provides a more specific version of the parent's behavior.

Quick recap

  • class Child is Parent declares inheritance — Child gets all Parent properties and methods
  • base(args) calls the parent constructor — use it to initialize parent properties
  • Child classes access parent properties by name directly — no re-declaration
  • Define a method with the same name as a parent method to override it
Copied!