Skip to main content
Inspiring
October 14, 2011
Question

Object oriented programming doubt

  • October 14, 2011
  • 1 reply
  • 680 views

Hi,

I have written 3 classes classA, classB and classC:

/* Class A */

package {

          public class classA {

                         public function classA() {

                                        trace("classA constructor");

                         }

 

                         public function doSomething():void {

                                        trace("classA.doSomething()");

                         }

 

                         public function knowMe():void {

                                        trace("classA.knowMe()");

                         }

          }

}

/* Class B */

package {

          import classA;

          public class classB extends classA {

                         public function classB() {

                                        trace("classB constructor");

                         }

 

                         public override function doSomething():void {

                                        trace("classB.doSomething()");

                         }

          }

}

/* Class C */

package {

          import classB;

          public class classC extends classB {

                         public function classC() {

                                        trace("classC constructor");

                         }

          }

}

And I have written this code in main timeline:

import classC;

var myClass:classC = new classC();

myClass.knowMe();

myClass.doSomething();

In this case the output is

classA constructor

classB constructor

classC constructor

classA.knowMe()

classB.doSomething()

Please let me know how can I refer to class A's doSomething method. ie to get output "classA.doSomething()"

This topic has been closed for replies.

1 reply

Inspiring
October 14, 2011

Call super doSomething in classB:

package

{

    import classA;

   

    public class classB extends classA

    {

       

        public function classB()

        {

            trace("classB constructor");

        }

       

        public override function doSomething():void

        {

            super.doSomething();

            trace("classB.doSomething()");

        }

   

    }

}

M_S_HAuthor
Inspiring
October 14, 2011

Thanks Andrei for response. Does that mean its not possible to directly refer to classA's doSomething method in AS3, like one can do in C++ using scope resolution concept?

Or is it possible to achive through use of interfaces?

Inspiring
October 14, 2011

You can access this method only on an instance of this class. Since there is no instance of the classA in your code - there is no way to access its API directly. In other words, the fact that you created an instance of subclass doesn't mean super class instance exists - only public/protected API is "transferred" to subclass. There is no scoping to super class in public subclass API. Of course, if you don't override the method - super's method will be called.

As a matter of fact, even private methods are not accessible by subclass.

Interfaces are just contracts and have no implementation.

Of you need a special access to super's method, you may need to create a special method that does it:

public function  callSuper_doSomething():void{

     super.doSomething();

}

In any case, why would you need this kind of functionality? I have a hard time to imagine a use case when it would be needed.