Have you ever wondered how the world would be if you had many moms and many dads? Ok, you might say that such a family structure isn’t such an impossibility nowadays – well guess what? Neither has it been for the longest time in AS3 and yet it seems very few are aware of Multiple Inheritance!

What I would like to share with you, first of all, is how Multiple Inheritance can facilitate some tasks for you and get more “bang” for your “bucks” in the long run.

Let’s say – You’re told by your client “I want a game of kitties and puppies!“, you sigh and ask “Ok how would you like the kittens?” -client: “I want robot kittens!“. Trying not to show that puzzled look in your face, you proceed and ask: “Ok sure, and how about the puppies?” -client: “I just want them like normal living puppies.

So from a programming standpoint – you think “Ok, so I gotta deal with living dogs (that eat and can be healed) and some kind of robot kittens (that recharge and can repair themselves)”. These are the two Interfaces you prepare for the two kind of species:

package com.bigp.oop_multi 
{
	
	/**
	 * IBiological
	 * @author Pierre Chamberlain
	 */
	public interface IBiological 
	{
		function eat():void
		function heal():void
	}
	
}
package com.bigp.oop_multi 
{
	
	/**
	 * IRobotic
	 * @author Pierre Chamberlain
	 */
	public interface IRobotic 
	{
		function recharge():void
		function repair():void
	}
	
}

K, but now at the last minute… the client gives you very specific technical details. Only the puppies should be visual (Sprite), while the kittens should only be allocated in memory for data runtime-tracking (Object). So here’s how our classes look like:

package com.bigp.oop_multi 
{
	import flash.display.Sprite;
	
	/**
	 * Dog class
	 * @author Pierre Chamberlain
	 */
	public class Dog extends Sprite implements IBiological
	{
		
		public function Dog() 
		{
			
		}
		
		/* INTERFACE com.bigp.oop_multi.IBiological */
		
		public function eat():void
		{
			
		}
		
		public function heal():void
		{
			
		}
		
	}

}
package com.bigp.oop_multi 
{
	/**
	 * Cat class
	 * @author Pierre Chamberlain
	 */
	public class Cat implements IRobotic
	{
		
		public function Cat() 
		{
			
		}
		
		/* INTERFACE com.bigp.oop_multi.IRobotic */
		
		public function recharge():void
		{
			
		}
		
		public function repair():void
		{
			
		}
		
	}

}

Neat! And then… when you though you were ready to fill the guts of those methods… the moody client says “Oh wait!… hmm could youuuu… actually make the Cats have the same behavior as the Dogs? Okay? Thanks!” and then drives off in his/her BMW to some important business meeting (a spa).

Before knowing about Multiple Inheritance, you might be tempted to copy-paste the functionality of one Class method into the other Class. Whoa-Whoa-whoa-whoa…. WHOAaaa! Hold it right there!

Are you seriously going to risk having to change your code again next time the client is “PMS’ing” to you? He/She could say that they don’t like this, or that, change it all… make it jump… make it sleep… make it check your email and answer the phone for you – what then huh? WHAT THEN?

Well let’s create a couple more files to prevent that from happening!

//IRobotic_code.as - flat ActionScript file:
public function recharge():void
{
	
}

public function repair():void
{
	
}

//IBiological_code.as - flat ActionScript file:
public function eat():void
{
	
}

public function heal():void
{
	
}

What happened exactly? We moved some stuff! Have a look back at the Dog and Cat classes:

//Cat
package com.bigp.oop_multi 
{
	/**
	 * ...
	 * @author Pierre Chamberlain
	 */
	public class Cat implements IRobotic, IBiological
	{
		
		public function Cat() 
		{
			
		}
		
		/* INTERFACE com.bigp.oop_multi.IRobotic */
		
		include "IRobotic_code.as";
		include "IBiological_code.as";
		
	}

}
//Dog
package com.bigp.oop_multi 
{
	import flash.display.Sprite;
	
	/**
	 * ...
	 * @author Pierre Chamberlain
	 */
	public class Dog extends Sprite implements IBiological, IRobotic
	{
		
		public function Dog() 
		{
			
		}
		
		/* INTERFACE com.bigp.oop_multi.IBiological */
		
		include "IBiological_code.as";
		include "IRobotic_code.as";
		
	}

}

Huh? Just like that? Yep… Just like that my friend! The secret here, is the include keyword as it is another way of “importing” code, only you can do it with portions and not necessarly a Class.

In case you’re wondering “Why didn’t you just extend both Classes from a super-class that has both functionalities already built-in?”. Well I would of too! But our needs here was to keep the Dog extending its own super class (Sprite) and the Cat class minds its own business as well (Object). Just figured it’d be worth mentionning – to justify the use of Multiple Inheritance :)

If you’re going to get as carried away as I am, you’ll soon see how useful this OOP structure allows you to spread functionality to all the objects in your games / applications.

My personal frequent use of it involves adding a “tween( params:Object )” method accessible by all my objects (DisplayObjects and general Objects alike). Maybe I’ll post a sample of my core framework one of these days ;)

But for now… time to shut some eyes and catch some sleep!

Good night all!