Types and OOP Inheritance

Inheritance and Interfaces

Klyn supports class inheritance, interface implementation, abstract members, and explicit calls to the parent implementation through super. These features keep object hierarchies structured without hiding control flow.

Extending a Class
import klyn.math

abstract class Shape:
    public property color as String

    public Shape(color as String = "black"):
        this.color = color

    public abstract area() as Double

class Circle extends Shape:
    public property radius as Double

    public Circle(radius as Double = 1):
        super("black")
        this.radius = radius

    public override area() as Double:
        return Math.PI * this.radius ** 2
Using super
class Base:
    public doSomething():
        print("base")

class Derived extends Base:
    public doSomething():
        super.doSomething()
        print("derived")
Constructor rule

When you call super(...) explicitly from a constructor, it must be the first effective call in that constructor body.

Constructor Delegation

Use this(...) as the first effective statement of a constructor to delegate initialization to another constructor of the same class. Overloads, named arguments, default values, and static type checks follow the regular constructor-call rules.

class User:
    public readonly property name as String
    public readonly property active as Boolean

    public User():
        this("Anonymous")

    public User(name as String):
        this(name, true)

    public User(name as String, active as Boolean):
        this.name = name
        this.active = active

Field initializers and the parent constructor run only in the terminal constructor. Delegation cycles are rejected at compile time.

Overriding

Use override when you want to replace inherited behavior and make that decision obvious in the source code.

public override toString() as String:
    return "Circle"
Abstract Classes and Methods

Abstract classes may define shared state and behavior while still leaving required operations unimplemented for subclasses.

abstract class Shape:
    public abstract area() as Double

Abstract types are not instantiable directly.

Implementing Interfaces
interface Runnable:
    public run() as Void

class Task implements Runnable:
    public run() as Void:
        print("running")

Interfaces describe required behavior. They may also carry static helpers when that improves the API shape.

Type Checks Across a Hierarchy
shape = Circle()
assert shape is Shape
assert shape is Circle
assert shape is not String