11. Classes & Objects
A class is a named type with fields (state) and methods (behavior). new ClassName(...) runs the constructor and returns an instance. Same file: Examples/OOP/classes_objects.malda.
11.1 A working class
The constructor has the same name as the class. Use this.field when a parameter shadows a field; elsewhere, member access can be implicit. Methods are functions declared inside the class.
class Person {
public var name;
public var age;
function Person(name, age) {
this.name = name;
this.age = age;
}
public function introduce() {
print("Hi, I'm " + name + " and I'm " + string(age) + " years old.");
}
public function getAge() {
return age;
}
public function setAge(newAge) {
age = newAge;
}
}
var person1 = new Person("Alice", 30);
var person2 = new Person("Bob", 25);
person1.introduce();
person2.introduce();
print("Alice's age: " + string(person1.getAge()));
person1.setAge(31);
print("Alice's new age: " + string(person1.getAge()));
new Person(...) unless class Person is declared in the same program (or imported). A snippet that only shows new Person is not a complete example.
11.2 Access modifiers
- public: accessible from anywhere (default for methods)
- private: accessible only within the class
- Fields default to private if no modifier is specified
11.3 Inheritance
A class can extend one parent with extends. Call super(...) from the child constructor, and super.method() to reuse a parent method. Same file: Examples/OOP/inheritance.malda.
class Animal {
public var name;
function Animal(name) {
this.name = name;
}
public function speak() {
print(name + " makes a sound");
}
}
class Dog extends Animal {
function Dog(name) {
super(name);
}
public function speak() {
print(name + " says: Woof!");
}
}
class Cat extends Animal {
function Cat(name) {
super(name);
}
public function speak() {
print(name + " says: Meow!");
}
}
var dog = new Dog("Rex");
var cat = new Cat("Fluffy");
dog.speak();
cat.speak();
To run the parent implementation as well, call super.speak() inside the override before or after the child body.
11.4 Static members
Static members belong to the class, not instances:
class MathUtils {
static var PI = 3.14159;
static function add(a, b) {
return a + b;
}
}
var pi = MathUtils.PI;
var sum = MathUtils.add(5, 3);
11.5 Operator overloading
Classes can overload selected operators by defining methods with reserved names. For binary operators, MALDA first tries the left operand overload (__add__, __sub__, etc.). If that overload is not found, MALDA then tries the right operand reversed overload (__radd__, __rsub__, etc.) with the left operand passed as argument. For unary operators, the single operand is the receiver.
| Operator | Left-hand method | Right-hand fallback method |
|---|---|---|
+ | __add__(other) | __radd__(other) |
- (binary) | __sub__(other) | __rsub__(other) |
* | __mul__(other) | __rmul__(other) |
/ | __div__(other) | __rdiv__(other) |
% | __mod__(other) | __rmod__(other) |
== | __eq__(other) | __req__(other) |
!= | __neq__(other) | __rneq__(other) |
< | __lt__(other) | __rlt__(other) |
<= | __le__(other) | __rle__(other) |
> | __gt__(other) | __rgt__(other) |
>= | __ge__(other) | __rge__(other) |
- (unary) | __neg__() | n/a |
If no matching left-hand or right-hand overload method exists, MALDA uses the built-in operator behavior.
class Value {
public var data;
function Value(data) {
this.data = data;
}
public function __add__(other) {
return data + other.data;
}
}
var a = new Value(2);
var b = new Value(3);
print(a + b); // 5
class RightValue {
public var data;
function RightValue(data) {
this.data = data;
}
public function __radd__(other) {
return other + data;
}
}
var rhs = new RightValue(7);
print(5 + rhs); // 12
11.6 Primary constructors
A parameter list after the class name is a primary constructor. Each parameter becomes a public field of the same name, and MALDA synthesizes a constructor that assigns those fields. The optional body is a normal class body (methods, extra fields, static members, operator overloads). There is no extra keyword: class Name( is distinct from class Name { and from class Name extends.
Those forms desugar to a classic class: public fields, a constructor with the same name as the class that assigns this.x = x (and so on), then any members from the body. Optional type hints on parameters are stored on those fields: class Point(x: float, y: float).
- Data-only:
class Point(x, y);orclass Point(x, y) { }. - Do not declare
function Point(...)in the same class (duplicate constructor). - Do not declare
var xin the body whenxis already a primary parameter. extendscannot be combined with a primary constructor. Use a classic class and callsuper(...).- Instances still use identity equality unless you define
__eq__.
class Point(x, y);
var p = new Point(3, 4);
print(p.x + p.y);
class Point(x, y) {
function total() {
return this.x + this.y;
}
}
print(new Point(3, 4).total());
See Also
- 4. Data Types - Object types
- 9. Functions - Methods vs functions
- 33. Examples - The same Person and Dog programs as catalog samples