Multi-Level Inheritance
Support deep inheritance chains with proper __index delegation.
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
endCreating 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 WoofMulti-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)
endCalling 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
endMethod 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
endDepth 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)) -- trueDeep 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.
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
- Base Class and Constructor Design
- Multi-Level Inheritance
- Mixin Composition and Interface Checks
- instanceof and Type Introspection