Welcome, Guest
  • Author Topic: Inheritance and constructor initiation  (Read 3817 times)

    Mattias Robo

    • Mods
    • Systems Administrator
    • *****
    • Posts: 555
      • View Profile
      • nook
    Inheritance and constructor initiation
    « on: 08/26/09, 08:38 »
    I have a class Sub, that inherits class Super.
    In Super I have a protected var with default value set that may be overwritten by Sub in Sub's constructor.
    (eg protected var foo:Number = 5)
    In the constructor of Super I want to do a check on that variable overwritten by Sub.

    First of all, am I right in the assumption that by default, the constructors of the different inherited classes are run in order of: top ancestor > bottom subclass?

    It seems that if I want the subclass constructor to run before the super class I can use the super() method. Is this right?

    In my example above I would have to use the super() method in the constructor of Sub since I need to set the variable before it is checked in the Super constructor.
    But this causes a problem.
    In the Sub constructor, if I trace the protected var the output is its native default value (in this case '0') as if it hasn't been set yet.
    I can change it, and if I trace it again it has the new value.
    But when the Super constructor runs after that, the variable is reset to what the Super class wanted it to be in the first place (in this case '5').

    Can someone please explain the nature of this behaviour?

    What would you recommend me to do? Obviously I could move the check to some init handler, but for various reasons I'd like to keep the checking in the constructor.

    cheers
         "I've figured it out. It's not the fords... it's what's IN them!.."

    kofi addaquay

    • Global Moderator
    • Senior Programmer
    • *****
    • Posts: 450
      • View Profile
      • Scripton Interactive
      • Email
    Re: Inheritance and constructor initiation
    « Reply #1 on: 08/28/09, 11:29 »
    a little code sample might help...how are you calling the super? are you just saying super()??? because dont forget...you can call super and pass arguments to it like

    super (arg1, arg2);

    or even a method in the super super.myMethod( arg1, 2)

    you can also override functions

    override public function myMethod( arg1, arg2) { };


    if this doesnt help...lets make up some test classes and find out what is going wrong.

    K

    Mattias Robo

    • Mods
    • Systems Administrator
    • *****
    • Posts: 555
      • View Profile
      • nook
    Re: Inheritance and constructor initiation
    « Reply #2 on: 08/31/09, 03:26 »
    Yes I know about all that, but it's not desirable in this case since there might be lots of different settings in different subclasses. I used a single one only for the example.
         "I've figured it out. It's not the fords... it's what's IN them!.."

    kofi addaquay

    • Global Moderator
    • Senior Programmer
    • *****
    • Posts: 450
      • View Profile
      • Scripton Interactive
      • Email
    Re: Inheritance and constructor initiation
    « Reply #3 on: 08/31/09, 08:32 »
    glad that worked for you... :)

    a little note about your assumptions concerning constructors of different inherited subclasses...

    when a subclass constructor method is provided, it is required to invoke its superclass constructor via super...note that the superclass constructor invocation occurs before any instance variables or methods are accessed...BUT if the super constructor isnt provided, the compiler adds a no-argument superclass constructor call automatically.

    so yes your assumptions were right...just wanted to give the reason why. Ok! take care ;)

    K

    Mattias Robo

    • Mods
    • Systems Administrator
    • *****
    • Posts: 555
      • View Profile
      • nook
    Re: Inheritance and constructor initiation
    « Reply #4 on: 08/31/09, 09:45 »
    Hmm.. maybe I misunderstood, but you don't have to call the super() method in a subclass constructor. I just tested using some traces and when no super() method is called the superclass construcor will still run, and before the subclass constructor. But if I add the super() method in my subclass constructor, the subclass will be initiated before the superclass, including variables.

    I'm also confused about your statement "the superclass constructor invocation occurs before any instance variables or methods are accessed", cause as I see it, all variables and methods are initiated before the constructor runs since I can use all my variables and method from within the constructor of the same class.
         "I've figured it out. It's not the fords... it's what's IN them!.."

    kofi addaquay

    • Global Moderator
    • Senior Programmer
    • *****
    • Posts: 450
      • View Profile
      • Scripton Interactive
      • Email
    Re: Inheritance and constructor initiation
    « Reply #5 on: 08/31/09, 22:28 »
    well, this is what i am saying Mattias...

    if you ask yourself the question what a subclass expected to do, you will come up with a few answers. like setting up variables defined by the subclass,... performing setup tasks related to that class.... as well as invoke the super class constructor...

    thats what lead me to say THIS superclass invocation must occur before any instance variables or methods are accessed :)

    for example...

    Code: [Select]

    public class Alpha
    {
    public function Alpha()
    {
    //
    }
    }
    //CASE 1
    public class Beta extends Alpha
    {
    //here we have the subclass constructor
    public function Beta()
    {
    //here we invoke the superclass constructor, we do this..
    super(); // THIS IS CALLED INVOKING EXPLICITLY
    }
    }

    //CASE 2
    public class Beta extends Alpha
    {
    //here we have the subclass constructor
    public function Beta()
    {
    //in this case, if a subclass does not define a constructor method. actionscript
    //AUTOMATICALLY creates one and adds a super call.
    }
    }


    now, even if you leave the super() call out! the super constructor it will get fired.


    here is a little test i did

    in the super class i have the following

    Code: [Select]
    protected var myVarA:Number = 13;
    protected var myVarB:Number;


    then in the Beta class i have this


    Code: [Select]

    public function Beta()
    {
                            trace("Beta constructor");
    //here we invoke the superclass constructor, we do this..
    trace("myVarA after super call:: " + myVarA);
    super.myVarA = 0;
    trace("myVarA after modifying protected:: " + myVarA);
    trace("myVarB:: " + myVarB);
    }



    here is what flash traces out

    Alpha constructor is fired
    Beta constructor
    myVarA after super call:: 13
    myVarA after modifying protected:: 0
    myVarB:: 90 // this comes from setting the myVar=90 in the super constructor


    there you see? the constructor of super gets fired first. before you set the value...its says myVarA is 13, THEN we set the myVarA to 0.


    so now, the question becomes...how were calling super? its probably something very little missing. anyways...i only wanted to play around with your theories. know you have a solution now.... thought we could play around a little bit :):)