class / extends / super
A class defines a type with methods and fields. extends declares inheritance from a parent class. super chains to the parent (constructor or method). The Bangla forms are শ্রেণী, প্রসারিত, and উপরের.
Defining a class
Methods take self as their first parameter explicitly — there is no implicit this. An init method, if present, runs on construction.
class Counter {
function init(self, start) {
self.value = start;
}
function increment(self) {
self.value = self.value + 1;
}
function get(self) {
return self.value;
}
}
var c = Counter(10); // no `new` keyword
c.increment();
c.increment();
print(c.get()); // 12
Notes:
- Construction is
ClassName(args)— there is nonewkeyword. - Method calls (
c.increment()) automatically pass the instance asself. - If a class has no
init, zero-arg construction works:Bag(). - Fields are set via
self.field = valueand read viaself.field(orobj.fieldfrom outside).
Inheritance with extends
class Animal {
function init(self, name) {
self.name = name;
}
function speak(self) {
return self.name + " makes a sound";
}
}
class Dog extends Animal {
function init(self, name, breed) {
super(name); // chain parent's init
self.breed = breed;
}
function speak(self) { // override
return self.name + " barks";
}
}
var d = Dog("Rex", "labrador");
print(d.speak()); // Rex barks
super(...) inside init calls the parent's constructor.
Calling parent methods
Use super.method(args) to delegate to the parent's version of a method:
class Dog extends Animal {
function describe(self) {
return super.describe() + " breed=" + self.breed;
}
}
You can chain through multiple levels — a subclass's super.x() resolves against its direct parent, regardless of how deep the hierarchy goes.
Multi-level inheritance
class Puppy extends Dog {
function speak(self) {
return super.speak() + " (puppy yip!)";
}
}
var p = Puppy("Buddy", "poodle");
print(p.speak()); // Buddy barks (puppy yip!)
What's not here
- No
this. Always useself, declared as the first method parameter. - No
new. Just call the class:Counter(10). - No
staticmethods. If you need a module-level helper, write a top-level function. - No access modifiers. No
private/protected/public— all fields and methods are accessible. - No multiple inheritance. A class extends at most one parent.
Bangla form
শ্রেণী Counter {
ফাংশন init(self, start) {
self.value = start;
}
ফাংশন increment(self) {
self.value = self.value + 1;
}
}
চলক c = Counter(10);
c.increment();
print(c.value); // 11