Home > ActionScript 3, Flash CS3, Tutorials, classes > ActionScript 3 Classes: Part 2

ActionScript 3 Classes: Part 2

You can find the code for this tutorial here: Classes_2.zip

One of the most important parts of Object Oriented Programming is that of creating classes. In the last tutorial on classes I went over how to create a simple class that represented a ball.

In this tutorial will be talking inheritance using simple definitions for shapes.

Inheritance means that you create a base or abstract class that has characteristics that are common and then create child classes that have characteristics of the base class plus their own individual characteristics.

For example:

You could create an object called Car and give it the properties of having four wheels, an engine, a windshield and so on. Then you would create child classes that inherited from the Car class called Truck, Sedan, Compact and they would have their own properties.

For this code example I will use Shape as the base or abstract class and then extend it to make two child classes called Circle and Square. Here is the code as follows:

This will be a file called Shape.as and would be in the same folder as the Circle.as and Square.as

package com.display
{
    public class Shape
    {
        public var color:String;

        public function Shape(color:String)
        {
            this.color = color;
        }
    }
}

Our shape class is pretty simple and only has one property: color. All of our shapes that we create will at least have a color. Let’s look at this line of code:

this.color = color;

this.color denotes that it’s the variable associated with the class and not the one being past into the function.

Now we’ll look at our two child classes:

Once again, this would be in a separate file called Circle.as. It’s important that you name your files the same as the class name or it won’t work.

package com.display
{
    public class Circle extends Shape
    {
        public var diameter:Number;

        public function Circle(color:String, diameter:Number):void
        {
            this.diameter = diameter;
            super(color);
        }
    }
}

This is block is in a file called Square.as that is in the same folder as Shape.as and Circle.as

package com.display
{
    public class Square extends Shape
    {
        public var width:Number;
        public var height:Number;

        public function Square(color:String, width:Number, height:Number):void
        {
            this.width = width;
            this.height = height;
            super(color);
        }
    }
}

Each of these classes have their own properties and the extend the Shape class. Meaning that although the Circle class has a diameter, it also has a color because it’s a child class of the Shape class.

Let’s look at the following line:

super(color);

Each of these classes have this line and all it means is that it’s calling the constructor from the parent class and passing the color value into it.

That’s all for this tutorial. Please leave comments about questions you may have.

  1. No comments yet.
  1. No trackbacks yet.