Actionscript Inheritance

Inheritance is a form of code reuse that allows programmers to develop new classes that are based on existing classes. The existing classes are often referred to as base classes or superclasses, while the new classes are usually called subclasses. It allows you to reuse code from a base class yet leave the existing code unmodified. It lets programmers conceptualize a group of classes in hierarchical term. For example, base class Car and Motorcicle have inherited from super class Vehicle because has a similar connotation.

Principles of Inheritance

This is a very simple example how inheritance works.

Main Class

/*
First we create simple instance of
Vehicle and outputs the variable and method
*/

var vehicle = new Vehicle();
trace(vehicle.wheels); //4
vehicle.firstMethod(); //outputs: firstMethod() was called        

/*
Then, because class Car extends (inherits) class Vehicle, instances of Car can automatically use
the method firstMethod( ) and the variable wheels, and also its methods.
*/

var car = new Car();
trace(car.wheels); //4
car.firstMethod(); //outputs: firstMethod() was called        
car.otherMethod(); //outputs: otherMethod() was called

/*
Because class MiniCar extends Car and Car extends Vehicle, class MiniCar can use firstMethod( ) and the variable wheels, otherMethod() of class car and also its methods
*/

var miniCar = new MiniCar();
trace(miniCar.wheels); //4
miniCar.firstMethod(); //outputs: firstMethod() was called        
miniCar.otherMethod(); //outputs: otherMethod() was called
miniCar.anotherMethod(); //outputs: anotherMethod() was called

Class Vehicle

package com.bananatools.tutorial{  
   public class Vehicle{  
      public var wheels:int = 4;      
      public function Vehicle(){
      }      
      public function firstMethod():void{
         trace("firstMethod() was called");
      }
   }
}

Class Car

package com.bananatools.tutorial{
   //to set up the inheritance relationship between
   //Car and Vehicle, we use the extends keyword: extends
   public class Car extends Vehicle{  
      public function Car(){        
      }      
      public function otherMethod():void{
         trace("otherMethod( ) was called");
      }
   }
}

Class MiniCar

package com.bananatools.tutorial{
   //to set up the inheritance relationship between
   //MiniCar and Car, we use the extends keyword: extends
   public class MiniCar extends Car{  
      public function MiniCar(){        
      }      
      public function anotherMethod():void{
         trace("anotherMethod( ) was called");
      }
   }
}

One important issue is instance methods and instance variables of a superclass, does not inherit its static methods and static variables to the subclasses.

Super classes can have several base classes, for example Motorcicle, Car, Bike, etc can inherit from Vehicle, not only one class.

Overriding Methods

If we want to override some method of the super class with another behavior, we just have to redefine (overrite) the method in the sub class.

Class Vehicle

package com.bananatools.tutorial{  
   public class Vehicle{  
      public var wheels:int = 4;      
      public function Vehicle(){
      }      
      public function firstMethod():void{
         trace("firstMethod() from Vehicle was called");
      }
   }
}

Class Car

package com.bananatools.tutorial{
   //to set up the inheritance relationship between
   //Car and Vehicle, we use the extends keyword: extends
   public class Car extends Vehicle{  
      public function Car(){        
      }      
      public function firstMethod():void{
         trace("firstMethod() from Car was called");
      }
   }
}

var vehicle = new Vehicle();
vehicle.firstMethod(); //outputs: firstMethod() from Vehicle was called

var car = new Car();
car.firstMethod(); //outputs: firstMethod() from Car was called

Constructor in Subclasses

As we know constructor initializes the instances of a class. When a class is extended, the subclass can define a constructor of its own or invoke the superclass constructor with the keyword "super".

Class Car

package com.bananatools.tutorial{
   //to set up the inheritance relationship between
   //Car and Vehicle, we use the extends keyword: extends
   public class Car extends Vehicle{  
      public function Car(){
             super(/*here, we can send parameters*/);
      }
   }
}

Well, i hope this tutorial help you to understand a little better ActionScript Inheritance Smile

0
Your rating: None

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd><p>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: [code], [blockcode], [as], [as3], [css], [drupal5], [drupal6], [html], [java], [javascript], [mysql], [php], [xml].
  • You may quote other posts using [quote] tags.
  • Textual smileys will be replaced with graphical ones.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.