Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6218401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:36:13+00:00 2026-05-24T07:36:13+00:00

I have two classes in Lua. One inherits another. test1 = {test1Data = 123,

  • 0

I have two classes in Lua. One inherits another.

test1 = {test1Data = 123, id= {0,3}}
function test1:hello()
    print 'HELLO!'
end
function test1:new (inp)
    inp = inp or {}
    setmetatable(inp, self)
    self.__index = self
    return inp
end
test2 = {}
function test2:bye ()
    print ('BYE!', self.id)
end
function test2:new_inst_test (baseClass, inp)
    inp = inp or {}
    setmetatable(inp, self)
    self.__index = self
    if baseClass then
        setmetatable( inp, { __index = baseClass } )
    end
    return inp
end

a = test1:new({passData='abc1'})
b = test1:new({passData='ghyrty'})

c = test2:new_inst_test(a,{temp = '123343321135'})
d = test2:new_inst_test(b, {temp = '1'})



print (c.temp, c.test1Data, c.passData)
print (d.temp, d.test1Data, d.passData)
c:bye()
c:hello()

I want test2 not just inherit test1, but save own methods (‘bye’).
Is it possible?
Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T07:36:14+00:00Added an answer on May 24, 2026 at 7:36 am

    you should set a metatable with __index=baseclass on the class metatable I think. But that will change the metatable for all objects in the test2 class. Doing it this way, you will use the methods from the class itself, and only use methods from the parent when the method does not exists in the current class, or it’s metatable.

    So it should be like

    if baseClass then
        setmetatable( self, { __index = baseClass } )
    end
    

    On the other hand it’s kind of weird that you only specify the baseclass when making a new instance, instead of specifying it when creating the new class.
    So I’d rethink how you inherit between classes instead of between instances and classes.

    As a small wizardry themed example:

    --oop.lua Example of OOP and inheritance in Lua
    Person={
        age=0,
        className='Person'
    }
    -- needed if needed add comparisons, operations, ...
    mtPerson={}
    mtPerson.__index={
        getClassName=function(self)
            return self.className
        end,
        new=function(self,t)    
            return setmetatable(t or {},{__index=self})
        end,
        inherit=function (self,t,methods)
            -- This is the heart of the inheritance: It says:
            -- Look it up in the methods table, and if it's not there, look it up in the parrent class (her called self)
            -- You pass this function the parent class (with :), a table of attributes and a table of methods.
            local mtnew={__index=setmetatable(methods,{__index=self})}
            return setmetatable(t or {},mtnew)
        end,
        introduce=function(self)
            print(("Hi! I'm %s, I'm a %s and I'm %d years old"):format(self.instanceName,self.className,self.age))
        end
        }
    
    setmetatable(Person,mtPerson)
    
    -- Wizard inherits from the class Person, and adds some default values and methods
    Wizard=Person:inherit({
        className="Wizard",
        knownSpells={},
        },
        {
        listSpells=function(self)
            print("known spells:",self)
            if #self.knownSpells==0 then
                print'none'
            else
                for k,v in ipairs(self.knownSpells) do
                    print(k,v)
                end
            end
        end
        }
    )
    
    i1=Person:new{
        inventory={'wallet'},
        instanceName="John",
    }
    
    i2=Wizard:new{ -- inherited method "new"
        inventory={'wallet','wand','cloak of invisibility'},
        instanceName="Harry",
        age=20,
        knownSpells={'Avada kavedra', 'Sesame open'}
    }
    
    i1:introduce() -- inherited method "introduce" notice that qge is the default value of 0
    i2:introduce() -- 
    
    i2:listSpells() -- method only in class 2
    i1.age=26
    i1:introduce()    -- changed age of instance
    print(Person.age)    -- didn't change class defaults
    print(Wizard.age)
    i1:listSpells() -- Error.
    

    While writing this, I came tho the conclusion that OOP in Lua is at the same time very simple, and very complicated. You simply have to really think things through before writing code, and stick with the plan afterwards. As such, here I chose to put attributes in the class and instance tables themselves, and put all methods in their respective metatables. I did this because now it’s easy to iterate through all attributes, without encountering methods, but any choice that works is valid. You just have to pick one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes. One(Person) for getters and setters, and another(People) for compute the
I have two classes, and want to include a static instance of one class
I have two classes that each need an instance of each other to function.
I have two classes. One is a management class, which stores a bunch of
I have two classes that are almost identical, besides one method. The classes have
I have two classes representing menu items. First one is Category , it represents
I have two classes which extend Activity and need to call another class method
I have two classes one and two . Both run threads. class Two is
I have two classes, Foo and Bar, that have constructors like this: class Foo
I have two classes: Media and Container. I have two lists List<Media> and List<Container>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.