Multiple Inheritance in ActionScript 3

17,819

Solution 1

Multiple inheritance is not possible in AS. But with interfaces you can mimic some of the functionality of multiple inheritance. MI has major flaws, most notably the diamond problem: http://en.wikipedia.org/wiki/Diamond_problem That's why many languages don't support MI, but only single inheritance. Using interfaces it "appears" you apply MI, but in reality that is not the case since interfaces don't provide an implementation, but only a promise of functionality.

interface BadAss{
    function doSomethingBadAss():void;
}

interface Preacher{
    function quoteBible():void;
}

class CrazyGangsta implements BadAss, Preacher{
    function quoteBible():void{
        trace( "The path of the righteous man is beset on all sides by the inequities of the selfish and the tyranny of evil men." );
    }
    function doSomethingBadAss():void{
        //do something badass
    }
}

var julesWinnfield : CrazyGangsta = new CrazyGangsta();
julesWinnfield.doSomethingBadAss();
julesWinnfield.quoteBible();

//however, it mimics MI, since you can do:

var mofo : BadAss = julesWinnfield;
mofo.doSomethingBadAss();
//but not mofo.quoteBible();

var holyMan : Preacher = julesWinnfield;
holyMan.quoteBible();
//but not holyMan.doSomethingBadAss();

P.S.: In case you'd wonder: There's no diamond problem with interfaces since an implementor of the interfaces must provide exactly one implementation of each member defined in the interfaces. So, even if both interfaces would define the same member (with identical signature of course) there will still be only one implementation.

Solution 2

Well there is no possibility of multiple inheritance directly in AS3 like many of other OOP languages.

OOP is about reusing code and like most of us want to reuse the code written in multiple classes. So if you really mean to reuse the code (logic) instead of just the signatures you might want to consider composition or deligation approaches and probably this is what you have read somewhere, like you said.

In composition what you do is instead of inheriting a baseclass into subclass, you will have an instance of the baseclass in a subclass and have all the methods

package
{
    public class BaseClass1
    {
        public function someMethod( )
        {
            trace("This is from the BaseClass1");
        }
    }
}

package
{
    public class BaseClass2
    {
        public function anotherMethod( )
        {
            trace("This is from the BaseClass2");
        }
    }
}
package
{
    //Composition
    public class HasBase
    {
        private var baseClass1:BaseClass1;
        private var baseClass2:BaseClass2;
        public function HasBase( )
        {
            baseClass1=new BaseClass1( );
            baseClass2=new BaseClass2( );
        }
        public function someMethod( )
        {
            baseClass1.someMethod( );
        }
        public function anotherMethod(){
            baseClass2.anotherMethod();
        }
    }
}

This is not a hack but actually a real and practical implementation adopted by experienced developers in many design patterns.

Hope this helps

Share:
17,819
Swati Singh
Author by

Swati Singh

Software Engineer | Blogger | Addicted to Learn New Technologies

Updated on July 26, 2022

Comments

  • Swati Singh
    Swati Singh almost 2 years

    Multiple Inheritance in ActionScript 3? Is it possible? I have read somewhere that it is possible in as3.

    If yes then how?

    this is my Doucument Class A.as

    package
    {
        import flash.display.MovieClip;
    
        public class A extends MovieClip implements B
        {    
            public var value1:Number=10;
    
            public function A()
            {
                trace("A Class Constructor");
            }
            public function hit():void
            {
                trace(value1+' from hit');   
            }
        }
    }
    

    Another is interface B.as

        package
        {
           public interface B
           {
              trace(' interface ');
              function hit():void;
           }
        }
    

    Thanks in advance.

  • Swati Singh
    Swati Singh over 12 years
    can you implement this in my example?
  • Mrugesh
    Mrugesh over 12 years
    Hi Swati, i can implement in your example. But can you send me your example code to me via email because in your question you mention only Interface B, Class A but not mentioned interface C. So I need code . my email address is [email protected]
  • Sang Park
    Sang Park over 12 years
    Technically this is not multiple inheritance which is not possible AS. However looking at the comment @swati-singh made avobe, it looks like he wants multiple interface and this would be a correct solution for that.
  • Swati Singh
    Swati Singh over 12 years
    Mrugesh: i have wriiten C by mistake. i have updated my code please check it. thanks for your reply.
  • Mrugesh
    Mrugesh over 12 years
    Which another class you want to extends with your class A?
  • Swati Singh
    Swati Singh over 12 years
    For now i just want to implement B Class. Because B is an interface.
  • Creynders
    Creynders over 12 years
    That's why I started my answer with: "Multiple inheritance is not possible in AS." :)
  • Mrugesh
    Mrugesh over 12 years
    In same package you can't create the Interface and Class with same name because both required same file name. you need to change your interface or class name for same package otherwise you need to change the your package for interface B and class B.
  • Mrugesh
    Mrugesh over 12 years
    Please check the edited answer with your example code i took class name C for another class and interface D for class C. if you have any concer then you can contact me via email or cell. you need my cell no then please write diwn comment or mail
  • Swati Singh
    Swati Singh over 12 years
    there is an error occurred: 1044: Interface method print in namespace D not implemented by class A.
  • Mrugesh
    Mrugesh over 12 years
  • Hyangelo
    Hyangelo over 12 years
    +1 for modeling CrazyGansta BadAss Preacher Jules using OOP. He won't be striking you down with great vengeance and furious anger.