Saturday, April 17, 2010

The language of Objects

I promised you last time that I would go over some of the terminology associated with object oriented programming (OOP) this time around. So let's do it using Alice. Start up Alice and create a new world using the Space template. Click the Add Objects button and scroll until you find the Space category and select it. Here is our first term associate with OOP, class. At the top of each entry in gallery it reads "Class something or other". Select Class LunarLander and add one to your scene.

Class and Instance - Making Objects
All OOP environments establish a class hierarchy. At the top, or root, of this hierarchy is usually a class called object. The object class isn't usually used to create actual objects, or instances, it is often called a meta-class. A meta-class is a class that is used to define other classes. OOP takes much of its structural ideas from how we deal with things in the real world. I'll use a real world example that I've used in the past when teaching programming courses.

For this thought experiment I want you to imagine that you have several writing instruments in front of you: a #2 pencil, a blue ball point pen, a red ball point pen and a black felt tip marker. Our object hierarchy will look something like this:

Object
  |---->WritingInstrument
               |---------->Pencil
               |---------->Pen
                            |---->BallPoint
                            |---->FeltTip


The classes don't create anything in our program, or in the case of Alice, in our scene. The class is a template, a definition of what an object might be. Two other terms you need to know, superclass and subclass. When we look at the hierarchy, WritingInstrument is a subclass of Object and the superclass of pen and pencil. With the exception of the root class all classes are a subclass of some other class. All classes except those at the bottom of the hierarchy are also superclasses of the classes below them. Each class can be used to create multiple instances or objects in our program or scene. Let's do that now. Return to your Space scene and add two astronauts to the scene with the LunarLander. When you return to the main Alice screen you'll see that the Object tree in the window looks something like this:
You'll notice that we have two separate objects, astronaut and astronaut2, these are instances of the astronaut class.

Properties and Methods - Defining an instance
One of the things that a class defines are properties of an object. Properties are those things that describe and define an object. They are often defined at the highest level in the hierarchy where they are appropriate. Going back to our writing instrument example the properties of color and tip would be defined at the WritingInstrument level. All WritingInstruments would have these two properties. A property such as InkType would be defined at the level of Pen. We can see from this that BallPoint and FeltTip probably wouldn't be separate classes. Both are members of the pen class but with a different value for the tip property.

Methods define how an object behaves. Continuing with the writing instruments the most important method would be the write method. This is the process of how the object writes on a paper or some other surface. This method would be defined at the level of the superclass WritingInstrument, but with no function. The pen class would redefine, or overload, the method with something like, "Applies ink to paper". A pencil on the other hand would have it's write method be, "Leaves trail of graphite on paper."  In Alice any actions we define are called methods.

Two other terms often associated with OOP are encapsulation and polymorphism. Encapsulation is the idea that the object is self contained. It carries all of the information within its definition to exist within the program. When properly constructed an object can exist in multiple programs without need of anything from the containing program to define itself. We've already discussed the concept of polymorphism. It's the idea that subclasses can redefine a property or method with more specificity than the superclass. For example pencils may define tip as have one of the following values: sharpened, mechanical, replaceable. A pen would have a tip that might be: ball point, felt, nylon or nib. (That last one is for the fountain pen users in the group.) So each has a tip property but it is defined specific to the subclasses needs.

Alright enough of the language lessons, let's get our astronauts doing something. First things first let's identify our astronauts. In the object tree right click astronaut and select rename. Change the object's name to Neil. Repeat the process for astronaut2 naming it Buzz. Click on the + next to Buzz and open the list of objects that make up Buzz. Find the helmet object and open that up. Select the visor object and open that. In the space below the object tree select the Properties tab. For color set the visor color to red. Repeat that process for Neil setting his visor color to blue. Now we can tell the astronauts apart.

Add the following to your My First Method:

  1. Drag a Do Together box into the window.
  2. Drag 2 Do In Order boxes into the Do Together box
  3. In the first Do In Order box add the following:
    1. Click on Neil, select the Methods tab and drag a Neil Turn method into the box. Have him turn 0.25 turn right.
    2. Drag the Wait option and set it below the turn option. Set the wait value to 1 second
    3. Drag a Neil Move method and set the distance to Other, 2 meters.
  4. In the second Do In Order box add the following:
    1. Click on Buzz and select the methods tab. Drag the Buzz Move method and set the values to Up 0.5 meters
    2. Add a 0.25 second wait
    3. Add Buzz Move down 0.5 meters
    4. Add Buzz Move up 1.0 meters
    5. Add a 0.5 second wait
    6. Add Buzz Move down 1.0 meters
  5. Below the Do Together box add a Do In Order box.
  6. Add the following to the Do in Order box:
    1. Using Buzz add: Buzz turn to face lunarlander.ladder
    2. Add Buzz Move To lunarlander.frontleg
    3. Add Buzz Move To lunarlander.ladder
    4. Click the Properties tab and drag the Opacity property to the Do In Order box and set the opacity to 0 (invisible)
    5. Add a 1.0 second Wait
    6. Using Neil add: Neil Turn to face lunarlander.ladder
    7. Add Neil Move To lunarlander.frontleg
    8. Add Neil Move To lunarlander.ladder
    9. Click Properties tab and drag the Opacity property to the Do In Order and set the opacity to 0.
Save your world and play the scene. Congratulations on your first moon walk.