8

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.

5
  • 7
    You only can extend from a single class, but you can implement as many interfaces as you like. What exactly are you trying to do when you say "Multiple Inheritance"? Commented Feb 2, 2012 at 5:16
  • Voted down for un-clear question. The person wants Multiple Implements Commented Feb 2, 2012 at 9:11
  • @WORMSS: this is called multiple inheritance using interface. Commented Feb 2, 2012 at 9:21
  • 1
    Its not inheritance.. The class is not inheriting.. What functionality have you inherited? None.. You have reported that there will be some publicly available methods on your class, that is it. No functionality behind those methods unless your class provides it itself. So therefore Multiple Implements not Multiple Inheritance Commented Feb 3, 2012 at 15:05
  • @wormss 100% correct, -1 for not asking clearly. your question makes sense without the code snippet. but the snippet making your question no sense. Commented Nov 22, 2012 at 10:17

4 Answers 4

72

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
That's why I started my answer with: "Multiple inheritance is not possible in AS." :)
+1 for modeling CrazyGansta BadAss Preacher Jules using OOP. He won't be striking you down with great vengeance and furious anger.
4

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

Comments

0

You can do the multiple inheritance using the interface.

  1. Define Class A,B and C. and Define interface for the Class B and C respectively iB and iC.
  2. Just extends the Class C to Class A using "extend" keyword - Direct Inheritance of class
  3. extends your interface iC to the interface iB.
Just check code as per your requirement:

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');
      }
      public function print():void
      {
          trace("Print Method Called");
      }
  }
} 

package
{
  public interface B extends D 
  {
      function hit():void;
  }
}

package
{
  public class C implements D
  {
      pulic function C()
      {
          trace("C Class Constructor");
      }
      public function print():void
      {
          trace("Print Method Called");
      }
  }
}

package
{
  public interface D
  {
      function print():void;
  }
}

9 Comments

can you implement this in my example?
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]
Mrugesh: i have wriiten C by mistake. i have updated my code please check it. thanks for your reply.
Which another class you want to extends with your class A?
For now i just want to implement B Class. Because B is an interface.
|
0

Umm, guys!

I can show you two different ways to "mimic" multiple inheritance:

  1. Using the #import feature: http://archive.darronschall.com/weblog/2006/10/multiple-inheritance-in-actionscript-3.html

  2. Don't forget that ActionScript is EcmaScript. It's still a prototype based language: http://jolierouge.net/2010/01/as3-multiple-inheritancemixins/

I know... not "real" MI, but they look like a duck, speak like a duck...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.