5 Replies Latest reply on Mar 24, 2010 12:16 AM by jake_flex

    Abstract Classes

    hidarikani Level 2

      Why doesn't AS3 support abstract classes? I'm used to creating abstract classes, can I substitute them with something similar in AS3?

        • 1. Re: Abstract Classes
          AdrianWiecek Level 3



          There's no substitute. In AS3 you can use interfaces which are more flexible, but do not provide features like abstract classes. There is a way to prevent users from creating classes, and force them to override methods(like throwing errors in body) but it is weird in AS3.




          • 2. Re: Abstract Classes
            msakrejda Level 4

            You can substitute them with regular classes and document that they are not to be instantiated directly and that some methods are to be overridden. It's a little ugly, but it works fine in practice.

            • 3. Re: Abstract Classes
              -BoNzO- Level 2

              Abstract classes are not found natively but can be emulated, and it's a widely use practise.


              example :


              public class AbstractStar


                   public function  AbstractStar()


                      // do stuff



                   public function shine() : void


                      throw new Error( "shine must be implemented" );   // Ideally you create a subclass of Error called AbstractmethodError or something



                   public function someAlreadyImplementedStuff() : void


                       // actual implementation



              1 person found this helpful
              • 4. Re: Abstract Classes
                Sebastien V. Level 3

                As Adrian said, one workaround is to throw errors in the body of abstract methods:


                protected function abstractMethod():void {
                     throw new Error( "Abstract method" );


                This allows you to spot immediatelly a non-overriden abstract method when running your application...

                • 5. Re: Abstract Classes
                  jake_flex Level 2

                  All the above posts are valid and work in practise. There's only one but. The check is done at run-time instead of compile-time as is the case with the languages I know that support abstract classes(Java and C++).


                  The problem with run-time checking is that you might have methods that are used quite rarely. You'd have to have unit tests for all your classes and methods. Unless there's a tool similar to PyChecker for ActionScript.


                  While I was doing more Python programming, I ran in into the same problem with absence of abstract classes. The solutions were pretty much same as here. One thing I'd like to point out though is that you can do just fine without abstract classes. It requires just a different approach and changing your mind away from the Java/C++ style of designing.

                  1 person found this helpful