next up previous contents
Next: Abtraction Up: Objects in Perl Previous: What are objects?

Classes

Before looking at specific examples of object-oriented Perl code, you need to see some generic examples. Looking at generic examples while learning the standard object-oriented terminology will ensure that you have a firm grasp of the concepts. If you had to learn new Perl concepts at the same time as the object concepts, something might be lost because of information overload.

Classes are used to group and describe object types. Character classes have already been looked with regular expressions A class in the object-oriented world is essentially the same thing. Let's create some classes for an inventory system for a pen and pencil vendor. Start with a pen object. How could you describe a pen from an inventory point of view?

Well, the pen probably has a part number, and you need to know how many of them there are. The color of the pen might also be important. What about the level of ink in the cartridge-is that important? Probably not to an inventory system because all the pens will be new and therefore full.

The thought process embodied in the previous paragraph is called modeling. Modeling is the process of deciding what will go into your objects. In essence, you create a model of the world out of objects.

Note: The terms object and class are pretty interchangeable. Except that a class might be considered an object described in computer language, whereas an object is just an object.

Objects are somewhat situationally dependent. The description of an object, and the class, depends on what needs to be done. If you were attempting to design a school course scheduling program, your objects would be very different than if you were designing a statistics program.

Now back to the inventory system. You were reading about pens and how they had colors and other identifying features. In object talk, these features are called properties. Figure 14.1 shows how the pen class looks at this stage of the discussion.

The Pen Class and its properties

Now that you have a class, it's time to generalize. Some people generalize first. I like to look at the details first and then extract the common information. Of course, usually you'd need several classes before any common features will appear. But because I've already thought this example through, you can cheat a little.

It's pretty obvious that all inventory items will need a part number and that each will have its own quantity-on-hand value. Therefore, you can create a more general class than Pen. Let's call it Inventory_item. Figure  14.2 shows this new class.

The Inventory_item class and its properties

Because some of Pen's properties are now also in Inventory_item, you need some mechanism or technique to avoid repetition of information. This is done by deriving the Pen class from Inventory_item. In other words, Inventory_item becomes the parent of Pen. Figure 14.3 shows how the two classes are now related.

The relationship between Inventory_item and Pen

You may not have noticed, but you have just used the concept of inheritance. The Pen class inherits two of its properties from the Inventory_item class. Inheritance is really no more complicated than that. The child class has the properties of itself plus whatever the parent class has.

You haven't seen methods or functions used in classes yet. This was deliberate. Methods are inherited in the same way that data is. However, there are a couple of tricky aspects of using methods that are better left for later. Perhaps even until you start looking at Perl code.

Note Even though you won't read about methods at this point in the chapter, there is something important that you need to know about inheritance and methods. First, methods are inherited just like properties. Second, using inherited methods helps to create your program more quickly because you are using functionality that is already working. Therefore, at least in theory, your programs should be easier to create.


next up previous contents
Next: Abtraction Up: Objects in Perl Previous: What are objects?
dave@cs.cf.ac.uk