Interpreter Project: Inheritance

This is the final section of the Crafting Interpreters Java implementation of the Lox language (which I actually implemented in Ruby). Inheritance is added as the final object oriented feature. This section was very short, so there isn't very much to talk about in this post.

As always you can see my code here.

Syntax

I like that Lox uses the same syntax as Ruby, a '<' character, to specify inheritance. It has always made sense to me. In my head it means the class name to the right is greater than the class to the left, implying a parent child relationship.

The other features are pretty standard. Calling a method on an instance of a class looks for a method of the same name in the current class, then searches upwards through the inheritance chain. If the method is not found a RuntimeError is raised.

Calling the super classes version of a method is also supported via the super keyword. This allows for something like:

class Vehicle {
  init(wheel_count) {
    this.wheel_count = wheel_count;
  }
}

class Car < Vehicle {
  init() {
    super(4);
    this.doors = 2;
  }
}

class Bicycle < Vehicle {
  init() {
    super(2);
  }
}

Moving Forward

This will be my last article in this series. I intend to continue working on interpreters and learning as much as I can about implementing languages. I want to write articles that will be more generally helpful going forward. I don't imagine it is terribly interesting reading about my experience implementing something. It would be more interesting to understand how to implement that thing.