flash oop_override

Override

The idea of override happens when we need to modify certain inherited functionalities. Imagine that an inherited method is not complete, we can override it in the sub-class in order to integrate our new version.

We will define a present method in our administrator class, in order to override the inherited version, to do this we use the keyword override:

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 administrator to present themselves
override public function present ( ):void
{
trace("I am a moderator");
}
}
}

By calling the present method on our moderator instance we trigger our redefined version:

// we instance a moderator
var myModo:Administrator = new Administrator("Michael", "Jackson", 48,
"Los Angeles");
// we ask him to present himself
// displays : I am a moderator
myModo.present();

In ActionScript 2, there is no keyword to override a method, the simple fact of defining a method with the same name in the sub-class overrides the inherited method. Thanks to the override keyword introduced by ActionScript 3 it is more simple for a developer to know if a method is an overridden method or not.

Beware, the overriding method should have the same name and the same signature as the overridden method, otherwise the override is said to incompatible. In the following example we return a value when the present method is executed:

package
{
// the inheritance is translated 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 administrator to present himself
override public function present ( ):String
{
// triggers the overridden method
super.present();
return "I am a moderator";
}
// method allowing the deletion of a player form a part
public function kickPlayer ( pPlayer:Player ):void
{
trace ("Kick " + pPlayer );
}
// method allowing the playing of a sound
public function jouerSon ( ):void
{
trace("Plays a sound");
}
}
}

The overriding method doesn’t have the same signature as the overridden method, compilation is impossible, if we test the following code:

var myModo:Administrator = new Administrator("Michael", "Jackson", 48,
"Los Angeles");

The following compilation error is generated:

1023: override not compatible.

In the event of an override, the overridden method isn’t lost. If we want to trigger the overridden method in our overriding method we use the super keyword:

package
{
// the inheritance is translated 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 player to present itself
override public function present ( ):void
{
// triggers the overridden present method
super.present();
trace("I am a moderator");
}
}
}

When the present method is triggered this also triggers the overridden version:

// we instance a moderator
var myModo:Administrator = new Administrator("Michael", "Jackson", 48,
"Los Angeles");
// we ask him to present himself
// displays :
/*
My name is Michael, I am 48 years old.
I am a moderator
*/
myModo.present();

In the previous code we increase the capabilities of the present method by adding the message “I am a moderator”. Thanks to the keyword super we can execute the overridden method, this is not lost.

By overriding using the empty method we can delete an inherited functionality:

package
{
// the inheritance is translated 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 );
}
// overriding method empty
override public function present ( ):void
{
}
}
}

When the present method is called on an instance of the administrator class the empty version is executed. This doesn’t contain any logic, we have cancelled the inheritance of the present method.

Worth remembering

  • Overriding allows you to redefine an inherited functionality.
  • In order for the overriding to be possible the overridden and overriding method should have the same signature.
  • To be able to override a method w use the keyword override.







Adobe Training center


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