Calling Parent Methods with super
Explicitly call base class methods for cooperative initialization.
Calling Parent Methods with super 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.
Why Super Calls?
When a child overrides a parent method but still needs the parent's logic, it calls the parent method explicitly. Lua has no super keyword — you reference the parent table directly: Parent.method(self, args).
local Base={}
Base.__index=Base
function Base:describe()
return "Base[name="..self.name.."]"
end
local Child={}
Child.__index=Child
setmetatable(Child,{__index=Base})
function Child:describe()
local parentDesc = Base.describe(self) -- super call
return parentDesc .. " + Child[extra=true]"
end
local c=setmetatable({name="X"},Child)
print(c:describe())Super in Constructor
Child constructors call parent constructors via Parent.new() or Parent._init(self,...). This ensures the parent's initialization runs for every child instance.
local Animal={}
Animal.__index=Animal
function Animal._init(self,name,sound)
self.name=name; self.sound=sound; self.alive=true
end
local Dog={}
Dog.__index=Dog; setmetatable(Dog,{__index=Animal})
function Dog.new(name,breed)
local self=setmetatable({},Dog)
Animal._init(self,name,"Woof") -- super init
self.breed=breed
return self
end
local d=Dog.new("Rex","Lab")
print(d.name,d.sound,d.breed)Cooperative Initialization
In deep hierarchies, each level initializes only its own fields and calls the parent's init. This keeps each class responsible for only its own fields.
local A={}
function A._init(self,x) self.x=x end
local B={}
function B._init(self,x,y) A._init(self,x); self.y=y end
local C={}
C.__index=C
function C.new(x,y,z)
local self=setmetatable({},C)
B._init(self,x,y)
self.z=z
return self
end
local c=C.new(1,2,3)
print(c.x,c.y,c.z) -- 1 2 3super Wrapper
Create a super(self) helper that returns a proxy object calling parent methods with self. This gives a cleaner syntax than Parent.method(self, ...).
local function super(self)
local mt = getmetatable(getmetatable(self).__index)
if mt then
return setmetatable({}, {__index=function(_,k)
local parentCls = mt.__index or mt
return function(...) return parentCls[k](self,...) end
end})
end
end
print("super() helper pattern shown")Multiple Levels
When there are multiple inheritance levels, each level calls its immediate parent. The chain propagates automatically.
local L1={} L1.__index=L1
function L1:intro() return "L1" end
local L2={} L2.__index=L2
setmetatable(L2,{__index=L1})
function L2:intro() return L1.intro(self).."+L2" end
local L3={} L3.__index=L3
setmetatable(L3,{__index=L2})
function L3:intro() return L2.intro(self).."+L3" end
local obj=setmetatable({},L3)
print(obj:intro()) -- L1+L2+L3Method Augmentation
Augment a parent method: call the parent version, then add extra behavior. This is "before" or "after" advice without a formal AOP framework.
local Logger={} Logger.__index=Logger
function Logger:log(msg) print("[LOG] "..msg) end
local TimedLogger={} TimedLogger.__index=TimedLogger
setmetatable(TimedLogger,{__index=Logger})
function TimedLogger:log(msg)
local ts=os.date("%H:%M:%S")
Logger.log(self,"["..ts.."] "..msg) -- augment
end
local tl=setmetatable({},TimedLogger)
tl:log("Server started")Avoiding Super Pitfalls
Always use the exact parent class name, not getmetatable(self).__index, for super calls — the metatable lookup can return an intermediate class, not the true parent. Be explicit for clarity and correctness.
-- Correct: explicit parent reference
function Child:init(x)
Base.init(self, x) -- explicit, clear
end
-- Fragile: uses metatable lookup
function Child:initBad(x)
local parent = getmetatable(getmetatable(self).__index)
-- This may not be Base if hierarchy changes!
end
print("Be explicit with parent class references")Super for __tostring
Override __tostring in a child while including the parent's representation for a complete description.
local Base={}
Base.__index=Base
Base.__tostring=function(self) return "Base("..self.name..")" end
local Child={}
Child.__index=Child
setmetatable(Child,{__index=Base})
Child.__tostring=function(self)
local baseStr = Base.__tostring(self)
return baseStr.." + Child(level="..self.level..")"
end
local c=setmetatable({name="X",level=5},Child)
print(tostring(c)) -- Base(X) + Child(level=5)Protected Template Method
The template method pattern: base class defines an algorithm skeleton with calls to abstract steps; subclasses implement the steps.
local Processor={} Processor.__index=Processor
function Processor:run(data)
data = self:preProcess(data)
data = self:process(data)
return self:postProcess(data)
end
function Processor:preProcess(d) return d end
function Processor:postProcess(d) return d end
function Processor:process(d) error("implement process()") end
local Upper={} Upper.__index=Upper
setmetatable(Upper,{__index=Processor})
function Upper:process(d) return d:upper() end
local p=setmetatable({},Upper)
print(p:run("hello")) -- HELLOSuper Call Convention
Convention: prefix internal-only init functions with _ (e.g. _init) and expose a public new() only at the top-level constructor. This keeps the inheritance chain clean.
-- Convention summary:
-- Animal._init(self, ...) -- used for super calls
-- Dog.new(...) -- only public constructor
-- Never call Dog._init from outside
local Foo={} Foo.__index=Foo
function Foo._init(self,x) self.x=x end
function Foo.new(x)
return setmetatable({},Foo):_init(x) or
(function() local s=setmetatable({},Foo); Foo._init(s,x); return s end)()
end
print("Convention: _init for super, new() for public")Super Call Question
How do you call a parent method in Lua?
Recap: Super Calls
Always call parent methods by explicit class reference: Parent.method(self, ...). Use _init convention for cooperative constructors across inheritance levels.
Frequently asked questions
Is the “Calling Parent Methods with super” lesson free?
Yes — the full text of “Calling Parent Methods with super” 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 “Calling Parent Methods with super”?
Explicitly call base class methods for cooperative initialization. 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 “Calling Parent Methods with super” 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
- Single Inheritance with __index Chaining
- Calling Parent Methods with super
- Mixin Patterns
- Overriding and Polymorphism