Classes & Object-Oriented Programming

Djazair provides an intuitive class-based OOP model with constructors (init), instance state (self), single inheritance (is), parent dispatch (super), and type verification (instanceof).

Declaring Classes & Instantiation

djazair
class Person
    init(name, age)
        self.name = name
        self.age = age
    end

    greet()
        print("Hi, I am ${self.name}, age ${self.age}.")
    end
end

# Instantiating with new
let p = new Person("Riad", 28)
p.greet() # => Hi, I am Riad, age 28.

Inheritance & super

Classes inherit using the is keyword. Child classes invoke parent constructors via super.init(...):

djazair
class Person
    init(name, age)
        self.name = name
        self.age = age
    end
end

class Employee is Person
    init(name, age, role)
        super.init(name, age)
        self.role = role
    end

    # Overriding method
    greet()
        print("Hello, I am ${self.name}, working as a ${self.role}.")
    end
end

let emp = new Employee("Sarah", 32, "Lead Architect")
emp.greet() # => Hello, I am Sarah, working as a Lead Architect.

Type Verification with instanceof

The instanceof operator verifies whether an object is an instance of a class, its superclasses, or primitive type names:

djazair
class Person
end

class Employee is Person
end

let emp = new Employee()

print(emp instanceof Employee)  # => True
print(emp instanceof Person)    # => True (inherited)
print(emp instanceof "String")  # => False

# Can also check primitive string types:
print("hello" instanceof "String") # => True
print(42 instanceof "Number")      # => True