6 Replies Latest reply on Dec 12, 2010 11:03 PM by Chaafii

    Understanding Override?

    Chaafii

      Hi, I'm new to Flex, but have some programming experience.  I'm currently working through a tutorial on Flash game development with Flex and I've seen something that has me baffled.  There's a certain subclass function that overrides a function of the parent.  In it, there's a line that reads, "this.shutdown();".  Now there's no shutdown function in the subclass, but there is in the parent class and I've used the Alert control to determine that it's being called.

       

      Am I to understand that a "this" reference in an overriding function refers to the object of the function that's being overridden?

       

      If so, this would suggest that the overriding function doesn't just accept outside calls to the overridden function, but that it actually overwrites it.  I could accept that but I've seen cases where an overriding function calls the overridden function for some basic logic before implementing the more specific logic of the subclass.

       

      Can someone please help me get a handle on this abstract, cuz it's hurting my head.

       

      Cheers

        • 1. Re: Understanding Override?
          Lee Burrows Level 4

          hi

           

          when you create a subclass, it inherits all the methods of its super class (its parent, in your terms) - you can optionally use override to redefine a super class method in the subclass

           

          in your example, if shutdown() doesn't appear (i.e. doesn't get overridden) in the subclass then it will get called on the super class instead - so yes, override does replace the super class method.

           

          if you override a method, you can still call the 'parent' method by using super

           

          eg: super.myMethodName(arg1, arg2);

          1 person found this helpful
          • 2. Re: Understanding Override?
            Chaafii Level 1

            Ok, it helps me if I can see it in my head as a model.  So, if I understand correctly, for the following simple example...

            --------------------------------------------------------

            package

            {

                 public class Parent

                 {

                      public function whatever():void

                      {

                      }

             

                      public function shutdown():void

                      {

                           [...shutdown code...]

                      }

                 }

            }

            --------------------------------------------------------

            package

            {

                 public class Child extends Parent

                 {

                      override public function whatever():void

                      {

                           this.shutdown;

                      }

                 }

            }

            --------------------------------------------------------

             

            1) Child's whatever function overwrites Parent's function in memory.

            2) Flex does something where it keeps Parent's whatever function somewhere else and it can be accessed using "super" from Child.

            3) If, for some reason, I needed to explicitly refer to Child from its own whatever function, I would use "this.Child".

             

            If those statements are all true, then I think I understand.

             

            Regards

            • 3. Re: Understanding Override?
              Lee Burrows Level 4

              you're nearly there. let me give you my example (i have avoiding using 'parent' and 'child' as these terms are usually used to refer to visual components - correct terminology for classes is super class [parent] and sub class [child]):

               

              package

              {

                   public class SuperClass

                   {

                        public function whatever():void

                        {

                             trace("whatever");

                        }

                        public function shutdown():void

                        {

                             trace("shutdown");

                        }

              }

              --------

               

              package

              {

                   public class SubClass extends SuperClass

                   {

                        public override function shutdown():void

                        {

                             trace("subclass shutdown");

                        }

              }

               

              --------

              say you create instances of each class:

               

              var sup:SuperClass = new SuperClass();

              var sub:SubClass = new SubClass():          

               

              then you can do:

               

              sup.whatever()  - traces "whatever"

              sup.shutdown() - traces "shutdown"

              sub.whatever()  - traces "whatever" (uses the super class definition - as it is inherited by the sub class)

              sub.shutdown() - traces "subclass shutdown" (uses the sub class definition - as it overrides the super class one)

               

              also, if you defined SubClass shutdown function like so:

               

               

              public override function shutdown():void

              {

                   super.shutdown();

                   trace("subclass shutdown");

              }

               

              sub.shutdown() - traces "shutdown" and "subclass shutdown"
              this is useful if you want to keep the behaviour of the super class function and add to it (rather than creating something completely different - which you can still do if you wish but that goes against the principles of object orientated design)
              one other point: you can only override public and protected functions, not private ones.
              does that make sense? feel free to keep the questions coming if not.
              • 4. Re: Understanding Override?
                Chaafii Level 1

                LOL, I'm still new to this and I haven't really gotten into trace yet.  I know it's for debugging and I'll eventually need to, but to this point, I've been using Alert messages.

                 

                But yeah, if it were me, I would structure it exactly as you did.  But the example I provided is an excerpt from a tutorial I'm trolling through and so I feel compelled to understand it and the implications it has on how override works.  The main point of it is that the subclass doesn't have a shutdown function and it accesses the super class' function from the "whatever" function by way of the "this" reference.  In effect, "this.shutdown();" works the same as if it were "super.shutdown();".  That's just so offensive to my sense of how OOP should and does work.

                 

                I know the distinction gets a little blurred, but super classes and subclasses are still distinct objects, right?  It's not that I need to (or probably could) understand this within the context of RAM architecture, but in general, each class instance (and all of its properties and methods) occupy their own block of memory, right?

                 

                The only way I can make sense of the example I provided is if "override" doesn't simply intercept calls to the super class' function, but in effect, overwrites it as if it had been coded there.  I mean how else can "this" not refer to the class where it is coded?  I appreciate your patience if I'm simply tripping on my own ignorance.

                 

                Cheers

                • 5. Re: Understanding Override?
                  Lee Burrows Level 4

                  hi. i think you're looking at it slightly wrongly? a subclass inherits all the public and protected methods and properties of its superclass, so if you dont override a method in the subclass, the code for the subclass method will be the same as for the superclass (ie: they are copies of each other) so calling this.myMethod() would do exactly the same as super.myMethod() - its only when a method is overriden that there would be a difference.

                  • 6. Re: Understanding Override?
                    Chaafii Level 1

                    Ah, cool.  I figured my post was bound for oblivion.  Thanks for the reply Lee.

                     

                    I figured it was some kind of brain fart on my part.  It's been a while since I've worked with subclasses and I was never an expert to begin with.  Thanks for clearing that up.

                     

                    All the best.