flash oop_typecasting
Typecasting
Typecasting is a simple process which consists of passing an object off for another type in the compiler. In order to understand typecasting let’s take the following situation. As we previously saw we can have a variable in the parent class storing instances of child classes.
In the following code we store an instance of the administrator class in the player type variable:
// creation of a Player and an Administrator var firstPlayer:Player = new Player ("Bobby", "Womack", 66, "Detroit"); var myModo:Player = new Administrator("Michael", "Jackson", 48, "Los Angeles"); // the kickPlayer method doesn’t exist for the compiler, as the player Class doesn’t define it myModo.kickPlayer(firstPlayer);
This doesn’t pose a problem, except when we try to call the kickPlayer method on the myModo variable, the compiler generates the following error:
1061: Call to a possibly undefined method kickPlayer through a reference with static type Player.
For the compiler, the player class doesn’t have a kickPlayer method, compilation is therefore impossible. Nevertheless we know that upon execution the myModo variable will contain an instance of the administrator class which has the kickPlayer method. In order to quiet the compiler we will use typecasting by telling the compiler “Trust me, there is the kickPlayer method on the object, leave me to compile”.
The typecasting syntax is simple, we specify the type then we place two brackets like for a traditional function:
Type ( myObject ) ;
We typecast therefore the myModo variable to the administrator type in order to be able to compile:
// creation of a Player and an Administrator var firstPlayer:Player = new Player ("Bobby", "Womack", 66, "Detroit"); var myModo:Player = new Administrator("Michael", "Jackson", 48, "Los Angeles"); // displays : Kick [Player name : Bobby, surname : Womack, age : 66, town : Detroit] Administrator ( myModo ).kickPlayer(firstPlayer);
On execution the method is executed. There again we can ask in what case we would need to rerun typecasting?
We will now define a new playerSound method on the administrator class which will be automatically called by the addPlayer method. When an administrator is added to the application a sound will be played in order to notify the arrival to the moderator:
package { // the inheritance is triggered by the keyword extends public class Administrator extends Player { public function Administrator ( pName:String, pSurname:String, pAge:int, pTown:String ) { super ( pName, pSurname, pAge, pTown ); } // method allowing the deletion of a player from the part public function kickPlayer ( pPlayer:Player ):void { trace ("Kick " + pPlayer ); } // method allowing the playing of a sound public function playSound ( ):void { trace("Play a sound"); } } }
In the playerManager class we can call the playSound method when an administrator type object is passed, to do this we test if the type matches then we call the desired method:
package { public class PlayerManager { // array containing the player references private var arrayPlayers:Array; public function PlayerManager ( ) { arrayPlayers = new Array(); } public function addPlayer ( pPlayer:Player ):void { pPlayer.present(); if ( pPlayer is Administrator ) pPlayer.playSound(); arrayPlayers.push ( pPlayer ); } public function deletePlayer ( pPlayer:Player ):void { var positionPlayer:int = arrayPlayers.indexOf ( pPlayer ); if ( positionPlayer != -1 ) arrayPlayers.splice ( positionPlayer, 1 ); else throw new Error ("Player not present!"); } public function getPlayers ( ):Array { return arrayPlayers; } } }
Once our classes are modified, we can intialise our application:
// creation of a Player and an Administrator var firstPlayer:Player = new Player ("Bobby", "Womack", 66, "Detroit"); var secondPlayer:Player = new Player ("Michael", "Jackson", 48, "Michigan"); var thirdPlayer:Player = new Player ("Lenny", "Williams", 50, "New York"); var myModo:Administrator = new Administrator("Michael", "Jackson", 48, "Los Angeles"); // management of players var myManager:PlayerManager = new PlayerManager(); // adding players and administrators myManager.addPlayer ( firstPlayer ); myManager.addPlayer ( secondPlayer ); myManager.addPlayer ( thirdPlayer ); myManager.addPlayer ( myModo );
On compilation the following error is generated:
1061: Call to a possibly undefined method playSound through a reference with static type player.
The compiler is not happy because it thinks that we are trying to call an existing method on the player class. We know that on execution, if the condition is not validated we will be assured that the object has the playSound method. To allow us to compile we typecast the administrator type:
package { public class PlayerManager { // array containing the player references private var arrayPlayers:Array; public function PlayerManager ( ) { arrayPlayers = new Array(); } public function addPlayer ( pPlayer:Player ):void { pPlayer.present(); if ( pPlayer is Administrator ) Administrator ( pPlayer ).playSound(); arrayPlayers.push ( pPlayer ); } public function deletePlayer ( pPlayer:Player ):void { var positionPlayer:int = arrayPlayers.indexOf ( pPlayer ); if ( positionPlayer != -1 ) arrayPlayers.splice ( positionPlayer, 1 ); else throw new Error ("Player not present!"); } public function getPlayers ( ):Array { return arrayPlayers; } } }
We can initialise our application:
// creation of a Player and an Administrator var firstPlayer:Player = new Player ("Bobby", "Womack", 66, "Detroit"); var secondPlayer:Player = new Player ("Michael", "Jackson", 48, "Michigan"); var thirdPlayer:Player = new Player ("Lenny", "Williams", 50, "New York"); var myModo:Administrator = new Administrator("Michael", "Jackson", 48, "Los Angeles"); // management of players var myManager:PlayerManager = new PlayerManager(); // adding players and administrators /* displays : My name is Bobby, I am 66 years old. My name is Michael, I am 48 years old. My name is Lenny, I am 50 years old. My name is Michael, I am 48 years old. Play a sound */ myManager.addPlayer ( firstPlayer ); myManager.addPlayer ( secondPlayer ); myManager.addPlayer ( thirdPlayer ); myManager.addPlayer ( myModo );
We will often come back to the concept of typecasting currently used in the display list.
Worth remembering
- You should not confuse conversion and typecasting.
- Typecasting doesn’t convert an object into another but allows the passing of one object off as another.
Mediabox Training Centre © 2000 - 2008 All rights reserved.
Adobe Authorized Training Centre. State convention under number 25 14 02167 14.
Mediabox : SARL au capital de 62.000€ - Activity number: 25 14 02167 14 - SIRET : 493 716 468 00027
MEDIABOX, 102 Avenue des Champs Elysées, 75008 PARIS - Tel. +33(0)2.31.91.96.89 - Fax. +33(0)2.72.68.56.42


