Lua Academy · Lesson

Multi-Level Inheritance

Support deep inheritance chains with proper __index delegation.

Lesson 2 of 413 steps

Multi-Level Inheritance is a free Lua Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, and your progress syncs across the web and the CoddyKit app. The Lua Academy course includes 4 lessons in total.

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.

Free to start

Learn Lua with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
40
Lessons
159

Frequently Asked Questions

Is the “Multi-Level Inheritance” lesson free?

Yes — any 3 lessons of the Lua Academy learning path, including “Multi-Level Inheritance”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Lua Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

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