Abstract Class in AS2.0

Page 1 of 1
Andrew Simmons
This user is yet to take control of their account and provide a biography. If you are the author of this article, please contact us via support AT actionscript DOT org.
View all articles by Andrew SimmonsWritten by: Andrew Simmons ,www.andrew-simmons.com andrew-simmons@andrew-simmons.com
Difficulty Level: advanced
Requirements: (MX 2004)
Topics Covered: OOP
Assumed Knowledge: Understanding of Interfaces in AS2.0
Unfortunately, abstract classes are not formally supported in ActionScript 2.0. However, remembering that interfaces are types, we may attain the full functionality of an abstract class.
Step One:
Create an interface that contains all the methods that would normally be abstract methods.
interface IAbstract
{
function myAbstractMethod();
}
Step Two:
Create the base class that will work as your abstract class. (The base class must cast a reference to itself as the interface type implemented by extending classes. See bellow.)
class Abstract
{
private var sub:IAbstract;
// Private constructor disallows instantiation
private function Abstract()
{
// Cast a reference of the extending class instance to the interface type
// which contains the abstract methods.
sub = IAbstract(this);
//Flash Player 7 Only
if(sub == null) // invalid cast will return null in Flash player 7
{
throw("Classes extending Abstract must implement IAbstract");
}
// end Flash Player 7 Only
/* For Flash Player 6 (or 7) use this runtime type check
if(!(sub instanceof IAbstract))
{
sub = null;
trace("Classes extending Abstract must implement IAbstract");
}
*/
//Because sub is of type IAbstract no compiler error is thrown
sub.myAbstractMethod();
}
}
Here is the foundation of this example. We store a reference to the current instance of the class extending Abstract in the variable sub .
sub = IAbstract(this);
This allows us to invoke the abstract method myAbstractMethod without a compiler error. If the extending class does not extend the interface, the cast returns null and we throw an exception. This forces the extending class to implement the interface IAbstract. The result is an uninstantiable class which contains some implementation and enforce specific and complete implementation by all deriving classes.
Step Three:
The extending class must simple extend the Abstract and implement IAbstract
class Extending extends Abstract implements IAbstract
{
// methods and properties here.
}
Spread The Word
Related Articles
1 Response to "Abstract Class in AS2.0" 
|
said this on 29 Sep 2007 7:02:03 PM CST
public interface Moveable
{ public boolean canMove(); public void move (Location loc); public Location getLocation(); } public interface VehicleTransporter { public boolean canLoad (Vehicle v); public void load (Vehicle v); public void unload (Vehicle v); } public class Location { private int x, y; /** * Constructor for objects of class Location */ public Location(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } } // end Location public abstract class Automobile extends Vehicle { public Automobile (float volume, float weight, Location location) { super(volume,weight,location); } } public abstract class Boat extends Vehicle { public Boat (float volume, float weight, Location location) { super(volume,weight,location); } } Part I You are given two classes, Vehicle and Location, and two interfaces, Moveable and VehicleTransporter. Add the rest of the classes shown in the picture below and establish the inheritance relationships shown. Note that Vehicle, Boat, and Automobile are abstract. Also, note that Vehicle implements Moveable, and Ferry and TransportTruck both implement VehicleTransporter. For now, don’t worry about implementing methods that make sense – simply write dummy methods and make sure that the whole structure compiles. For example, if you are required to add a method called foo() that returns a Boolean value, it is enough (for now) to write it as follows: public boolean foo() { return false; } |



Author/Admin)