Inheritance in Lua

Hi fellows corona SDK developers,

I am currently trying to implements inheritance with Beebe’s approach explained in his tutorial blogpost: Modular Classes in Corona found here: http://blog.anscamobile.com/2011/09/tutorial-modular-classes-in-corona/

I’m familiar at how using classes in corona, but soon as I try to implements inheritance, I am getting some unexpected errors.

In the comment section of his blogpost, fellow @Victor wanted to know how to implement inheritance, just like me. Jonathan gave him this answer:


@Victor: To make the dog class extend from that, in your dog.new() function, you’d set the table be an instance of animal, and also have whatever new properties that are specific to that dog. For example:

<br>function dog.new()<br> local d = require( "animal" ).new()<br> d.name = "Rover"<br> <br> return setmetatable( d, dog_mt )<br>end<br>

But, when I try this inheritance approach, I got an error while trying to call the numberOfLegs() methods.

I would like someone to look at my code and see what’s going wrong. Maybe Beebe’s approach is correct and I am just doing it wrong, but I want to know how I can implement inheritance in Lua the best way possible, possibly in line with Beebe’s approach tutorial…

Here I post the code from an easy and well commented example:

main.lua :

--Regular use of the base class animal.lua  
  
local animal = require("animal").new({  
 legs = " 4 legs !",  
 })  
  
  
--Call method numberOfLegs()  
animal:numberOfLegs() -- Output: The animal has : 4 legs !  
  
--Use of inherited class dog  
local dog = require("dog").new()  
  
--Call method getName()  
dog:getName() -- Output: His name is Rover  
  
--Call method numberOfLegs()  
dog:numberOfLegs() -- ERROR : attempt to call method 'numberOfLegs' (a nil value)  
--I should be able to call base methods from an inherited class...but it throws me an error...  

dog.lua (inherited from animal.lua) :

--Class dog  
  
local dog= {}  
local dog\_mt = { \_\_index = dog } -- metatable  
  
-- PUBLIC FUNCTIONS  
----  
  
function dog.new() -- constructor  
  
local dog = require( "animal" ).new({legs = " 4 legs !"})  
 dog.name = "Rover"  
  
 return setmetatable( dog, dog\_mt )  
  
end  
  
function dog:getName()  
 print("His name is " .. self.name)  
end  
return dog  

animal.lua (base class) :

--Class animal  
  
local animal= {}  
local animal\_mt = { \_\_index = animal } -- metatable  
  
-- PUBLIC FUNCTIONS  
----  
  
function animal.new( params ) -- constructor  
  
local newAnimal = {  
legs = params.legs,  
hair = params.hair or false,  
vertebrate = params.vertebrated or true  
}  
  
return setmetatable( newAnimal, animal\_mt )  
  
end  
  
---  
  
function animal:numberOfLegs ()  
print ( "The animal has :" .. self.legs )  
end  
  
return animal  

[import]uid: 20617 topic_id: 24462 reply_id: 324462[/import]

I suspect is has something to do with the metatable, but it’ll take some time to perfect your example.

In the meantime, I recommend you take a look at this module:

http://lua-users.org/wiki/KlassModule

It seems to be a very easy to use class module that gives you at least everything my modular classes tutorial did, plus more. It is most-likely more in-line with other OOP conventions as well. [import]uid: 52430 topic_id: 24462 reply_id: 99043[/import]

I hope you can give me an answer because honestly, I really don’t understand a bit about your klass module and how I should use it. Jugling between base functions is not very my cup of tea and I was maybe hoping for a more simplistic solution. If you can by any way post a test example of this module and explain me how to make a more simple class with, It would be very kind ^^

thank you Jo
Ray [import]uid: 20617 topic_id: 24462 reply_id: 99354[/import]