Chapter 3: The Notion of a Class and Some Other Key Ideas


OVERVIEW

Every branch of knowledge has its special words and phrases that capture the essence of that knowledge. The following words/phrases represent the concepts that are most central to OO programming:

  • Class

  • Encapsulation

  • Inheritance

  • Polymorphism

Objects in object-oriented programming (OO) are made from classes. At a high level of conceptualization, a class can be thought of as a category or a type. We may think of "Cat" as a class. A specific cat would then be an object or an instance of this class. At the level of programming, a class is a data structure with data members for representing the various properties of the different object instances of the class, and with member functions for representing the behavior of the class. For example, the class Cat could be given the following definition:

    class Cat {         string name;         double weight;         // other data members         void meow() {              // code for sounding out a meow         }             void purr() {              // code for sounding out a purr         }             // code that would allow a specific         // cat to be instantiated from this class,         // etc.    } 

In this definition, name and weight are the data members of the class Cat, and meow() and purr() its member functions. The data members of a class are also known as the fields of the class and the member functions as methods.[1]

So the concept of a class essentially helps us pull together the various properties and behaviors important to a certain category of objects.

In object-oriented programming, there often arises a need to "hide" some of the implementation detail of a class and to also control access to some of the data members and the member functions of a class. If direct access to a data member or a member function has the potential of putting an object in an undesirable or an erroneous state, then obviously you would not allow direct access to such data members and member functions. If the implementation code for meow() for a Cat object requires that we define another function regulateExhale(), we probably would not want other objects in a program to access regulateExhale() directly. Before invoking regulateExhale(), the function meow() probably has to place the cat in a state where it has inhaled. An outsider having an unfettered access to regulateExhale() could cause the cat to exhale on an empty lung.

Hiding, or controlling the access, of the implementation-related data members and member functions of an object is called encapsulation. With appropriate data encapsulation, each object will present a well-defined public interface for its clients (the users of the object). A client would only be able to access those data members and invoke those members functions that are in the public interface.

The other two concepts, polymorphism and inheritance, rely on us being able to establish a hierarchy of classes for the different objects needed by a program. To illustrate the notion of a hierarchy of classes, let's start with the class Animal which stands for what it means. This class can be extended into a more specialized class FourLegged representing just the four-legged animals. The class Animal could also be extended to a more specialized class TwoLegged. When a class A is extended to create a class B, we say that class B is a subclass of class A and that A is a superclass of B. We can also say that class A is the base class and class B the derived class.

A set of classes related through such superclass—subclass relationships forms a hierarchy, as illustrated by the example in Figure 3.1. In particular, a class hierarchy formed in the manner shown in the figure is an IsA hierarchy, in the sense every Cat IsA FourLegged, every Dog IsA FourLegged, every FourLegged IsAn Animal, and so on.

click to expand
Figure 3.1

Inheritance in object-oriented programming allows a subclass to inherit some or all of the data members and functions of its superclass(es). This is one of the most important reasons for why object-oriented code is more easily extensible than other kinds of code. If a vendor-supplied class does not fit the bill exactly, you can extend it by creating a subclass that would inherit the properties and the behavior of the vendor-supplied class and then you can add to it additional properties and behaviors as needed.

That brings us to the notion of polymorphism. Polymorphism basically means that a given category of objects can exhibit multiple identities at the same time, in the sense that a Cat object is not only of type Cat, but also of types FourLegged and Animal, all at the same time. What does that gain us? Since every Cat is a FourLegged and every FourLegged is an Animal, it should be possible to "manipulate" the Cat objects through the functions defined for the Animal class. As an example of what we mean by manipulation here, suppose we have a function calculateIQ() defined for all of the classes in the animal hierarchy shown above. Obviously, the actual implementation of calculateIQ() for a Cat would be very different from its implementation for a Dog or a Duck. So, presumably, all we can do is to declare calculateIQ() for the root Animal class, have each subclass inherit the function declaration from the root class, and, if at all possible, provide its own implementation. We will also make sure that calculateIQ() is accessible to the users of the animal hierarchy of classes.[2]

Now let's make an array of animals as follows:

      Animal[] animals = {kitty, fido, tabby, quacker, spot}; 

where kitty and tabby are Cat objects, fido and spot Dog objects, and quacker a Duck object. So even though the objects in the array are of different actual types, we want to be able to invoke those functions on them that are defined for the Animal class since all the objects are after all of type Animal also. Polymorphism lets us do exactly that. It lets us treat kitty as a Cat, as a FourLegged, and as an Animal, all at the same time. Thus, through polymorphism, we can write object-oriented code like

      in i;      for ( i = O; i < animals.length; i++ )           animals[i].calculateIQ(); 

Polymorphism would cause the correct implementation code for calculateIQ() to be automatically invoked for each of the animals. The function invoked for kitty would be the calculateIQ() defined for the Cat class, for fido the same function but defined for the Dog class, and so on.

So, in a nutshell, polymorphism allows us to manipulate objects belonging to the different classes of a hierarchy through a common interface defined for a root class. Polymorphism allows for different definitions for functions of the same name to reside in different class and for automatic invocation of object-specific definitions of such functions at run time. This, as we will see later, can lead to great efficiencies in programming.

It is time for the reader to become acquainted more formally with what's meant by a class in C++ and Java. At this time, our explanations and examples will be elementary, their primary role being to facilitate introduction of other ideas in the chapters that immediately follow this one. Later in the book, after we have gone through many other associated ideas, we will revisit the concept of a class in Chapter 11 and provide richer examples there.

[1]Strictly speaking, a member function can be called a method only under certain conditions having to do with polymorphism. The distinction will be clarified in Chapters 9 and 15.

[2]This would allow us to say that calculateIQ() is a part of the public interface of the class hierarchy.




Programming With Objects[c] A Comparative Presentation of Object-Oriented Programming With C++ and Java
Programming with Objects: A Comparative Presentation of Object Oriented Programming with C++ and Java
ISBN: 0471268526
EAN: 2147483647
Year: 2005
Pages: 273
Authors: Avinash Kak

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net