0Pricing
Lua Academy · Lesson

Multi-Level Inheritance

Support deep inheritance chains with proper __index delegation.

Single Inheritance Review

Single inheritance is achieved by setting Child.__index = Parent. Method lookup: if not in Child, look in Parent via __index.

Extending Class Factory for Inheritance

Add an :extend() method to the base class that creates a subclass linked to the parent.

function Animal:extend()
  local cls = {}
  cls.__index = cls
  setmetatable(cls, {__index = self})
  cls.super = self
  function cls:new(...)
    local inst = setmetatable({}, cls)
    if inst.init then inst:init(...) end
    return inst
  end
  return cls
end

All lessons in this course

  1. Base Class and Constructor Design
  2. Multi-Level Inheritance
  3. Mixin Composition and Interface Checks
  4. instanceof and Type Introspection
← Back to Lua Academy