In addition to objects, two other concepts that are fundamental to the object oriented paradigm are classes and inheritance. Classes are a way of specifying abstract data types in the object oriented paradigm. However, classes differ from the traditional concept of abstract data types in that they support inheritance. Inheritance allows new data types (i.e., classes) to be defined as extensions of previously defined data types [Balci 1998]. (For more information on abstract data types, see the Abstract Data Types lesson in the Data Structures module).

To illustrate these new concepts, consider the list object that was discussed in the previous lesson. This object stored a list of numbers and allowed for common operations such as inserting a number, deleting a number, and sorting the list. The diagram to the right shows a list object with an array of four integers to hold the list data. What happens when a designer needs more than one list in a particular design? Clearly the single list object cannot store the data for all the lists. On the other hand, all the lists perform the same operations so they are closely related to each other. To solve this problem, what the designer really needs is a list data type that will serve as a template for creating all the list objects. Using the list data type, the designer can create multiple list objects that share the same behavior but have their own private data. In the object oriented paradigm, such a data type is called a class. The List class specifies the storage requirements for the private data of each list object, and the List class also defines the behavior of the objects by specifying the methods each object will use to access and manipulate its private data. Once the designer has completed the List class, he or she can use this class to create many similar list objects. The creation of a new list object is called instantiation. Each list object that is instantiated is distinct from the other list objects and has its own storage space for private data. The diagram below illustrates three list objects that have been instantiated from the List class. Notice that each object which was instantiated contains its own private data but shares the same methods that are specified in the class.

Using classes it is possible to model a wide range of entities, not just the simple data structures presented so far. For example, consider the design of a drawing program using the object oriented paradigm. The drawing program will need to support the following figures: lines, circles, triangles, and rectangles. These figures can be modeled by creating a class for each figure. Since each figure will consist of various colors and sizes and will have a unique location, these attributes must be represented. Also, the class methods must provide a way for the drawing program to manipulate the attributes of the figures. The table below shows some hypothetical classes for each of the figures. For illustration purposes, only a subset of the attributes and methods needed to accurately model the figures is presented.

Class Name
 Line  Circle  Triangle  Rectangle
Private Data
x, y location
x, y location
x, y location
x, y location
line color
line color 
line color 
line color 
length               
     fill color fill color fill color
     eccentricity          
     radius          
          number of sides number of sides
          base length
          height width
Methods
Move location
Move location
Move location
Move location
Change length               
     Change fill Change fill Change fill
     Compute area Compute area Compute area

The private data for the figure classes are composed of the various attributes of the figures. Notice that some of the attributes such as line color are shared by all the classes while some of the other attributes such as eccentricity (a measure of roundness) are specific to a certain class, namely the Circle class. The methods for the figures include actions to move the location of the figure, change the length, change the fill color, and compute the area. Like the attributes, some of the methods presented are also class specific. The method Compute area, for instance, cannot apply to a line.

Take another look at the Circle, Triangle, and Rectangle classes in the table below. The common attributes and methods are now highlighted. Many attributes and methods are shared in common by these three classes. Rather than repeating these characteristics in each class, a better design would be to abstract the shared characteristics into a separate class which can serve as the the base class of Circle, Triangle, and Rectangle. This design approach is called inheritance.

Class Name
 Circle  Triangle  Rectangle
Private Data
x, y location
x, y location
x, y location
line color 
line color 
line color 
fill color fill color fill color
eccentricity          
radius          
     number of sides number of sides
     base length
     height width
Methods
Move location
Move location
Move location
Change fill Change fill Change fill
Compute area Compute area Compute area

A new Closed Figure class is created by abstracting the common attributes and methods from Circle, Triangle, and Rectangle. Closed Figure now becomes the base class for these classes and they are derived from the Closed Figure class. The diagram below pictures the relationship between base classes and derived classes with lines that represent inheritance. The arrows show that Circle, Triangle, and Rectangle all inherit from Closed Figure, that is, the Closed Figure class passes on its characteristics (i.e., its attributes and methods) to the three figure classes. Recall that the purpose of inheritance is to define new classes as extensions of previously defined data types. Because the shape classes are inheriting from Closed Figure, they are really extending and specializing this class. Be sure not to confuse inheritance and instantiation. Inheritance involves a relationship between different classes while instantiation involves the creation of an object from a class.

Class Name
 Closed Figure
Private Data
x, y location
line color 
fill color
Methods
Move location
Change fill

Although the Closed Figure class is one way to capture the common characteristics of the Circle, Triangle, and Rectangle classes, this solution has some notable problems. First, the Circle class is fundamentally different from the Triangle and Rectangle classes. The circle really belongs to a group of figures known as ellipses while the other two classes belong to a group of figures known as polygons. Such differences are an indication that new classes are needed to properly model these figures. Second, the Line class shares some characteristics in common with the other three figures, but not enough to be derived from the Closed Figure class. This problem is an indication that the Closed Figure class is too comprehensive. It models attributes that really belong in another class that can serve as a base class for all four of the figure classes. The animation below addresses these problems and presents a better class hierarchy for modeling figures.

[View in New Window]

The table below summarizes eight of the most important concepts in the object oriented paradigm.

Object a collection of private data and a set of operations that can be performed on that data [Balci 1996].
Method an operation for accessing and manipulating data within an object.
Message a request for a specific action to be performed by a specific object.
Interface a collection of methods which specify how messages can be sent to an object.
Encapsulation users see only the services available from an object, but not how those services are implemented [Balci 1998].
Class a way of specifying abstract data types in the object oriented paradigm.
Instantiation creation of a new object from a class.
Inheritance defining new classes to be extensions of previously defined classes.

References