0Pricing
Lua Academy · Lesson

Multi-Level Inheritance

Support deep inheritance chains with proper __index delegation.

Multi-Level Inheritance is a free Lua Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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

Creating a Subclass

Create Dog as a subclass of Animal, overriding init and adding new methods.

local Dog = Animal:extend()
function Dog:init(name)
  Dog.super.init(self, name, "Woof")
  self.tricks = {}
end
function Dog:learnTrick(trick)
  self.tricks[#self.tricks+1] = trick
end
local d = Dog:new("Rex")
d:learnTrick("sit")
d:speak()  -- Rex says Woof

Multi-Level Chain

Extend Dog to create GuideDog — a three-level inheritance chain.

local GuideDog = Dog:extend()
function GuideDog:init(name, owner)
  GuideDog.super.init(self, name)
  self.owner = owner
end
function GuideDog:guide()
  print(self.name .. " guides " .. self.owner)
end

Calling super Methods

Always call super through the class reference, not through self, to avoid infinite recursion.

function Dog:init(name)
  Dog.super.init(self, name, "Woof")  -- explicit super call
end

Method Resolution Order

Lua resolves methods bottom-up through __index chains. GuideDog → Dog → Animal → nil. The first found wins.

Overriding Methods

Define a method with the same name in the subclass to override. Call super explicitly for partial overrides.

function Dog:speak()
  print(self.name .. " barks!")
  Dog.super.speak(self)  -- also call parent speak
end

Depth Limits

Very deep inheritance chains slow method lookup (each level adds one __index step). Prefer composition for more than 3-4 levels.

Checking Class Membership

Walk the metatable chain to check if an instance belongs to a class.

local function isA(instance, cls)
  local mt = getmetatable(instance)
  while mt do
    if mt == cls then return true end
    mt = getmetatable(mt)
  end
  return false
end
print(isA(d, Dog))     -- true
print(isA(d, Animal))  -- true

Deep Copy with Class Preservation

When deep-copying instances, preserve the metatable so the copy is still an instance of the correct class.

When to Prefer Composition

Use inheritance for true is-a relationships (Dog is-a Animal). Use composition (component tables) for has-a relationships or flexible behavior mixing.

Inheritance Question

How does multi-level inheritance work in Lua?

Recap: Multi-Level Inheritance

Extend the class factory with an :extend() method that chains __index links. Call super explicitly with ClassName.super.method(self, ...). Check membership by walking the metatable chain.

Frequently asked questions

Is the “Multi-Level Inheritance” lesson free?

Yes — the full text of “Multi-Level Inheritance” is free to read here on the web, and the Lua Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Lua Academy course, upgrade to CoddyKit PRO.

What will I learn in “Multi-Level Inheritance”?

Support deep inheritance chains with proper __index delegation. You practise Lua Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Lua Academy?

No prior experience is required. Lua Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Multi-Level Inheritance” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Lua Academy lesson?

Yes. Every Lua Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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