Introducing Object Orientation

This week you'll advance your understanding of ActionScript into one of the most powerful areas of coding: object orientation (OO). Like algebra, double-entry bookkeeping, and winemaking, object orientation ranks among the most significant inventions in human history. Also like those other things, it's somewhat complicated and best learned by practice; but before you dive in, it's necessary to understand some history and basic concepts.

When and why OO?

The first object-oriented programming language was called Simula, created in 1967 to help programmers of digital computers model physical phenomena. The influences driving evolution of OO were both internal to computing (arrival of larger computers and of more powerful interactive control systems) and located outside in the world (complex problems such as nuclear physics, aircraft design, and space flight).

Object orientation displaced a number of earlier approaches, most notably structured programming, which tended to organize instructions into large blocks or libraries (often with numbered lines), identifying certain passages of this code as modules or subroutines. This practice lacked the flexibility and scaling inherent in OO. By the mid-1980s, the OO concept of classes had been added to the popular programming language C to create C++. Ten years later, Sun Microsystems released Java, an object-oriented language designed for use on the Internet. The revision of ActionScript in version 2.0 makes this scripting language considerably more consistent with Java.

In very real ways, fundamental concepts in modern computing such as packet switching, distributed applications, and multitasking all reflect object-oriented concepts. Some people have also seen OO as a starting point for "biological" computing, code systems that replicate and evolve in ways that mimic the operation of DNA (if only distantly). Though it's easy to take this comparison too far, it is often useful to think about objects and classes as dynamic environments or ecologies.

At the heart of OO lie three basic tenets: polymorphism, inheritance, and encapsulation. We'll explain each of these below, but not before we've first glossed two even more fundamental concepts, objects and classes.

What is an object?

Objects are the foundation of OO thinking. They are discrete combinations of data and procedures, properties and methods. The Movie Clips and other Symbols you have been manipulating in Flash are very much like objects, especially if they have attached functions and variables. The key to understanding objects is to recognize that objects combine two modes: representation (variables, properties) and transformation (methods, functions); or if you like, noun and verb.

What is a class?

In your first encounters with Flash you learned how to convert simple graphics into Symbols by placing them in the Library. You could then drag multiple copies of a single Symbol onto the Stage. You could also open the Library from one project or main movie and import Symbols from it into another project.

In this way, Symbols in Flash resemble classes. A class is a generalized or abstract definition of an object from which duplicate and/or modified versions may be generated. The derivative of a class is called an instance. Flash uses this term in referring to instance names of Movie Clips.

Unlike Flash Symbols, however, the OO classes with which we'll work are not defined primarily through the visual user interface, but through coding. You'll learn a particular syntax for class definitions, including a method called a constructor for creating instances of the class.

What is polymorphism?

Though it might sound like a chronic disease, polymorphism is actually a major source of OO's power: it's the ability of objects to include variations from their originating class. This lets us adapt basic class definitions to create objects that vary with changing conditions of a program, simulation, or game.

What is inheritance?

Inheritance is a convention whereby instances of a class have access to the methods and properties defined in the original. This lets us create many copies of an object, all starting with standard features and behaviors.

What is encapsulation?

Encapsulation is the third basic tenet of object orientation. In simpler language it is sometimes called information hiding. It means that the basic logical structure of a class (its properties and methods) are not spelled out in the code that creates an instance. Typically instance code will contain specific data (new values for the properties, for example) that is not needed in the class definition.

How do I work this?

The three major features of OO -- and particularly that last one -- require some mental preparation and adjustment. So, some practical advice:

  • Get comfortable with complexity. You'll need to develop the ability to remember how one chunk of code relates to another, even when that other chunk isn't immediately before you. Often you will have multiple script documents (files with the extension .as) open at once. Tabs are your friend.

  • Learn to think in parallel. Though scripting is inherently sequential, like all aspects of language, and though most PCs process only one instruction at a time, the fast speed of execution creates a plausible illusion of parallelism. You will need to think about multiple events happening at the same time. Some people thus compare programming to musical composition or orchestration. It's a useful analogy.

  • Sometimes a bug is a feature. There will be times when your object-oriented scripts do not behave as you thought they would. When the results are ugly or obviously wrong, you have a bug. But on occasion you may discover an interesting or useful behavior you did not intend. This phenomenon is called emergence and can be a marvelous thing. However, avoid the temptation to invoke emergence in every emergency. Sometimes a bug is just a particularly nasty creature.

University of Baltimore Logo

Copyright © 2004 School of Information Arts and Technologies