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.
ActionScript 3 is an object-oriented programming language originally created by Macromedia Inc., which continued to evolve after being acquired by Adobe Systems. It is a superset of the ECMAScript standard (more widely known as JavaScript) with a stronger focus on classes, interfaces, and objects.
2006–2020: ActionScript 3.0 In June 2006, ActionScript 3.0 debuted with Adobe Flex 2.0 and its corresponding player, Flash Player 9.
(Deprecated with Animate) ActionScript 1.0 is the simplest form of ActionScript, and is still used by some versions of the Adobe Flash Lite Player.
Both JavaScript and ActionScript are scripting languages, or interpreted languages. The browser (or Flash) interprets the code at runtime. They are not compiled (like C++ or Java).
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With