Increasing Reusability - Classification and inheritance Print E-mail
Wednesday, 31 January 2001 09:24
Article Index
Increasing Reusability
Classification and inheritance
Shape class hierarchy
Multiple inheritance
All Pages

Classification and Inheritance

In OOP, shared behavior is achieved by defining some kind of template of what a particular object looks like. In the vast majority of object-oriented systemsPrototyping is the other, less popular, means of achieving object-oriented behavior sharing. Basically, after an object is defined, another similar object will be defined by referring to the original one as a template, then listing the new object’s differences from the original. Act1 and DELEGATION are object-oriented programming languages that use prototyping for behavior sharing rather than classification. Hybrid and Exemplars use both methods. In prototyping systems, objects themselves are the templates, while classification systems use classes as templates for objects. The classification approach is so predominant in OOP that many people would define objects as encapsulations that share data by classification and inheritance. However, I have chosen to use the expression “behavior sharing”, as this includes the alternate technique of prototyping., this is done by defining a template called a class (but see Okoli 1996). Every object created belongs to a class. Every object in a class has exactly the same kind of data members and methods, so once you know an object’s class you know exactly what the object is like.

Classes are hierarchical, which means that classes have superclasses and subclasses. Look at the following example from high school geometry:

 

 

The Rectangle class is a subclass of the Quadrilateral class, which is a subclass of Polygon, which is a subclass of Shape. Going the other way, Shape is Polygon’s superclass, which is Quadrilateral’s superclass, which is Rectangle’s superclass.

In a class hierarchy, every class has a superclass except for the class at the top of the hierarchy; this called the root class. In our example, Shape is the root of the hierarchy. From the other end, the classes that don’t have any subclasses, like Rectangle and Isosceles are called leaf classes. (Funny names? Well, think of a class hierarchy as an upside-down tree; then you’ll get the picture.)

Notice that a single class can have more than one subclass, as does Polygon. This makes sense, since there are many kinds of polygons. In fact, other than triangles and quadrilaterals, there are also pentagons, hexagons, and many other shapes with multiple sides. But a class hierarchy for solving a programming problem doesn’t have to include every conceivable class, only those that will likely be necessary.

The OOP class system is a lot like the biological classification system for living things: If I tell you that a drill is a large, light brown animal with a short tail, you might have no idea what a drill looks like. If I tell you it’s a monkey, then you immediately get a picture in your mind, because you know what all monkeys generally look like; so you simply color your mental monkey light brown, make it large, and give it a short tail. You wouldn’t confuse a drill with a mastiff, a large, light brown, short-tailed dog, because a dog is a different kind of animal from a monkey. (To see how good your mental imaging is, compare this drill and mastiff with what you had in your mind.)

Similarly, if I tell you that I have an object called someShape in our shape hierarchy whose sides are 4 inches each, could you draw it? Not really, at least not until I further tell you that someShape is a heptagon. Then you know it’s a member of the Polygon class with seven sides of 4 inches each, and you know exactly what it looks like. (Note that you can create an object whose class is anywhere along the hierarchy; it doesn’t have to be a leaf class like Rectangle or Isosceles.)

It’s important to understand that an object is distinct from its classIn some OOPLs like Smalltalk and Java, classes are objects themselves. This feature gives some powerful programming flexibility, but even in this case a class is never its own class; rather, a special class is defined in these languages to classify classes. Does that make your head spin? I understand.. The class Rectangle describes what rectangle objects look like and what they do, but it’s not the same thing as a rectangle object. The programmer creates objects that are instances of a particular class. For example, you could create three objects of the Rectangle class called r1, r2 and r3. These are rectangle objects, but none of them represents the idea (class) “Rectangle”. In our biology example, a drill is an instance of a Monkey, but not all Monkeys are drills. In object-oriented jargon, creating an object that is an instance of a class is called instantiation.



Last Updated on Tuesday, 28 October 2008 14:51