Introduction: Inheritance vs. Composition
My interest in Flash started mostly because of a Jib-Jab cartoon ("This Land") in 2004. I'm the author of a feature I call "Mazoons," which are a combination of mazes and cartoons. In 2002, I even had a book published, "Super Silly Mazes." I'm not a professional programmer, but making my mazes interactive by programming them with Flash became a hobby/obsession of mine, to the point where I have now learned more than I bargained for. Lately I'm working on a new website about Flash and Actionscript 3.0 called The Flash Connection.View all articles by Jody Hall
You might have wondered why I would have you create a class that has a MovieClip as a property, instead of just creating a class that is a MovieClip. The first technique is called "composition" and the second "inheritance." This is not to imply that this only applies to MovieClips, it's just that MovieClips make a great, easy to understand example of it. So, the difference between composition and inheritance (in the case of MovieClips) is the difference between whether an object HAS A MovieClip as a property, or whether the object IS A MovieClip. So the former two tutorials were both examples of the first technique, composition, even though I didn't tell you that at the time.
I did it this way for several reasons: First, I knew that a lot of people would already be familiar with drawing something on the stage, converting it to a symbol, and giving it an instance name. That being the case, I could take you from what you already knew, and then go into how to use a class to control that instance. Secondly, I wanted you to realize that although a MovieClip is an object, certainly not all objects are MovieClips. An object is basically a custom datatype, and can be composed of anything you want, even instances of other classes. So, it was to get you to start thinking in terms of objects, but not to confuse objects with MovieClips. And the final reason: composition is a valid technique, and it allowed me to demonstrate at the end of the first tutorial that you could control the same MovieClip instance using two different classes.
I'd also like to clarify something here. I don't have an OOP background, don't know design patterns or anything (yet, haha!), and am really just getting started with classes myself. I had just begun to get into using classes in AS2, just before AS3 came out. I don't do flash development for a living, rather it's a hobby of mine at the moment. However, since I'm basically a beginner with OOP and AS3 myself, and I've had to dig to find out a lot of things, I feel that rather than disqualifying me, this actually helps me to better relate these things to other beginners. While I can't bring you past whatever level I'm at, what I can do is explain the things that I've learned in terms you can understand, because I've experienced it too, and not all that long ago. Sound good? Read on.
The next page will explain how to use the other technique, inheritance, to link a MovieClip in the library to a class file.