Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
overriding parent's function in child class — Gideros Forum

overriding parent's function in child class

bysregbysreg Member
edited December 2012 in General questions
straight to the code snippet :

--A.lua
A = Core.class()

function A:init()
self:foo()
end

function A:foo()
print("x")
end

--B.lua
B = Core.class(A)

function B:init()
end

function B:foo()
print("y")
end

local b = B.new()

The output is "x"

shouldn't it print "y" ?

i thought that it was because of the different self bug mentioned at my older thread here : http://www.giderosmobile.com/forum/discussion/1677/object-in-super-class039-init-function-is-not-the-same-as-in-the-inherited-class039-init-function-#Item_3

and here : http://www.giderosmobile.com/forum/discussion/1234/accessing-children-from-parent-class#Item_14

but now it seems it's already got the same self object but still it doesn't call the overridden function

thank you

Comments

  • ar2rsawseenar2rsawseen Maintainer
    edited December 2012
    @bysreg no it shouldn't because foo method of B class is not called anywhere.
    Basically the order goes like this.

    You initiate B class,
    it initiates A class,
    it calls foo method of A class, because it's in its constructor
    then returns back to B init method,
    does everything that needs to be done there
    and returns instance.

    And this jumping happens only in constructor, so now if you call
    b:foo()
    it will not call A:foo method :)

    What effect are you trying to achieve? Maybe there's another way
  • @ar2rsawseen

    so it is really designed to be like that ?
    afaik, at least in other true OO language, the foo called in A will call the foo defined in B as the object b is the instance of class B which inherits A
  • @bysreg

    Yes, I get you, in C#, for example, the B:foo method would have been called and if you haven't specifically said to call A:foo method it won't be called.

    But since in Lua OOP is not native, but only programmed on top of it, it has it's own restrictions, but also a lot's of other new capabilities and is very, very, veeerrryyy flexible. :)
  • @ar2rsawseen

    all right, thank you for your fast help.

    Yes, then i guess Gideros class can make it more OO-like like the above suggested code output? but for now, i think i'll think of a different way to achieve the same effect
  • @bysreg,

    Lua is quite fun and flexible once you understand the way it works.

    However think of what you are trying to do like an Abstract Class in C++ so your new class will have all of the functions by default, you can override them with new functions, however when you use
    B = Core.class(A)
    A is like a super class and on init, the
    A:init
    is invoked first. If you want to invoke foo on class B, you will have to explicitly invoke it as
    b:foo()
    or in the
    B:init()
    function.

    This is how it unfolds
     b = B.new()
     --> A:init()
     --> A:foo()
     --> prints x
     
     --> B:init()
    Hope this would help you understand what happens and where you need to place your functions for what you need.
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
  • So as far as I understand from the discussion going on here, there is no way to skip the execution of code from parent class' init() ?
  • @twisttap,
    if you have worked with C++/Java, when you inherit, there is always the super or the parent and if I recollect correct, the super is always called in most of the functions.

    If you need that the parent class is not called, do not inherit from it. Unless I am missing something here.
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
Sign In or Register to comment.