Basic OOP concepts: Inheritance vs. composition
There are just a handful of concepts at the core of OOP. This article covers the most important ones: inheritance, encapsulation, and polymorphism. I also discuss a few related topics to help you put these ideas into context.
Without a doubt, inheritance is the most well-known principle of OOP. Inheritance can be defined as the ability to inherit properties and methods and extend the functionality of an existing class in a new one.
If you're thinking ahead, you might imagine creating a new "Wall" class that extends the "Brick" class you created earlier. However, that is not how inheritance works.
Looking at the relationship between a brick and a wall, the best way to code this is not through inheritance but rather by a concept called composition.
A simple rule of thumb determines whether the relationship between classes is one that warrants inheritance or composition. If you can say class A "is a" class B, you're dealing with inheritance. If you can say class A "has a" class B, the relationship is one of composition.
Here are some examples of inheritance:
- Cat "is an" animal
- Engineer "is an" employee
- Rugby "is a" sport
Here are examples of composition:
- Wall "has a" brick
- Computer "has a" keyboard
- School "has a" teacher
So what is the difference in how inheritance and composition are implemented? Let's compare how this works, starting with inheritance:
Animal.as
package com.adobe.ooas3 {
public class Animal {
public var furry:Boolean;
public var domestic:Boolean;
public function Animal() {
trace("new animal created");
}
}
}
The Animal.as code is the base Animal class, which you will now extend using inheritance with a Cat class:
Cat.as
package com.adobe.ooas3 {
public class Cat extends Animal {
public var family:String;
public function Cat() {
furry = true;
domestic = true;
family = "feline";
}
}
}
If you look at the Cat class, the constructor assigns values to three different properties. On close inspection, only one of these properties (family) is defined in the Cat class. The other properties (furry and domestic) come from the Animal base class.
While this not exactly the most practical example, you can see how class inheritance allows you to build upon existing functionality to create a new blueprint for you to start using as you develop your project.
Now if you wanted to create half a dozen cats, you could simply do this by instantiating the Cat class, which has all the properties already set up, rather than using the generic Animal class and having to define the properties again and again for each instance.
New in ActionScript 3.0 is an override
keyword that is used when (you guessed it) you want to override a method defined in the class that you extended. This useful feature prevents you from accidentally running into naming conflicts with methods between classes that extend each other.
On the other hand, composition doesn't have any formal syntax like the extends
keyword. Composition simply instantiates its own instance of any class it wants to use.
Let's take the Brick class created earlier. In this next example you'll create a Wall class that uses composition to instantiate instances of the Brick class:
Wall.as
package com.adobe.ooas3 {
import com.adobe.ooas3.Brick;
public class Wall {
public var wallWidth:uint;
public var wallHeight:uint;
public function Wall(w:uint, h:uint) {
wallWidth = w;
wallHeight = h;
build();
}
public function build():void {
for(var i:uint=0; i
In the code above, the Wall class accepts two arguments passed to its constructor, defining the width and height in bricks of the wall you want to create.
Let's do a quick test of this class by instantiating it on the main Timeline of a blank FLA file:
import com.adobe.ooas3.Wall;
var myWall:Wall = new Wall(4,4);
If you run Test Movie (Control > Test Movie), you'll see that 16 Brick instances are created with corresponding trace
statements displayed in the Output panel to create a 4 x 4 wall (see Figure 2).
Figure 2. Output panel of Wall class getting executed
Apart from the difference in class relationship between inheritance and composition (as I discussed earlier), composition has the advantage of being able to add functionality to another class at runtime. It allows you to have control over the creation and destruction of class instances, whereas with inheritance the relationship between the classes is fixed and defined at the time the code is compiled.
No comments:
Post a Comment