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 override doSomething():
super.doSomething()
print("derived")
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
The override modifier is mandatory when an instance method replaces a method
inherited from a parent class, including a generic method, an abstract method or a method of Object.
The parameter signature—including any ref or out passing modes—must match, the return type must be compatible, and visibility
cannot be reduced. An overriding method or interface implementation cannot strengthen an inherited parameter contract by adding notnull when the parent or interface declaration permits null. A final method cannot be overridden.
public override toString() as String:
return "Circle"
Constructors and new overloads do not use override. Implementing an interface
method for the first time does not require it. Conversely, using override
without a matching inherited method is a compilation error.
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