Contents

Roo

Inheritance

A class can inherit from another class. The class that is inheriting is called the subclass and the class that is inherited from is the superclass. When a class inherits from another it gains all of the methods, getters and properties already defined in the superclass. Roo supports single inheritance. Inheritance is specified in the class declaration with the < operator:

class Person {  
    greet(someone) {
        print("Hello " + someone)
    }   
}

class MadScientist < Person {
    exclaim {
        print("Great Scott!")
    }
}

var emmett = MadScientist()
emmett.greet("Marty") # => "Hello Marty"
emmett.exclaim # => "Great Scott!"

If a superclass defines an init() method, this is not inherited by the subclass. It can however be accessed by the subclass using the super keyword:

class Person {
    init(name) {
        self.name = name
        print("Hello from the Person init() method")
    }
}

class MadScientist < Person {
    init(name) {
        super.init(name)
        print("Hello from the MadScientist init() method")
    }
}

var emmett = MadScientist("Doc Brown")
# Prints:
# "Hello from the Person init() method"
# "Hello from the MadScientist init() method"

Any methods defined in the superclass can be access with the super keyword.

You can override methods defined in the superclass:

class Person {
    greet {
        print("I'm a person")
    }
}

class MadScientist < Person {
    greet {
        print("I'm a mad scientist!")
    }
}

var marty = Person()
var emmett = MadScientist()

marty.greet # => "I'm a person"
emmett.greet # => "I'm a mad scientist!"