Chapter 6: Abstraction


You probably heard the story of the multimillionaire who told her son that he would inherit the family fortune if he promised to continue working after she passed on. Mom dies. The money starts pouring in, and somehow the son forgets about his promise. However, a clause in her will forces him back to work. So what does this have to do with abstraction? It is abstraction! Abstraction is a way a programmer of a super class (mom) forces a programmer of a subclass (son) to define a behavior. You ll learn about the whys and hows of abstraction in this chapter.

Abstraction: The Enforcer

Let s begin exploring abstraction by revisiting the concepts class , super class , and subclass , which you learned about in Chapter 5. A class contains data and behaviors that are associated with an object. For example, a student ID is data, and registering a student is a behavior that is likely to be found in a Student class.

Programmers define a super class whenever two or more objects use data and a behavior. A super class is Java s version of a base class in C++. The super class is then inherited by other classes that require the data and behavior. These classes are called subclasses in Java and derived classes in C++.

For example, the UndergradStudent class and the GradStudent class are likely to inherit the Student class in order to access the student ID and registration behavior, as well as other data and behaviors defined in the Student class (see Figure 6-1).

click to expand
Figure 6-1: The Student class is the super class and is inherited by the UndergradStudent class and the GradStudent class, which are subclasses.

To avoid confusion, we ll use the terms super class and subclass in this chapter, but everything we say about them also applies to a base class and derived class in C++.

A behavior defined in a super class is referred to as default behavior because it specifies instructions that are followed by all subclasses that use the behavior. A programmer who defines a subclass has the option of using a default behavior or redefining the behavior to suit the needs of the subclass.

Let s say that graduate students have an entirely different registration process than other students. Therefore, the default registration behavior in the super class cannot be used by the subclass. The programmer who defines the subclass ignores the default registration behavior and defines a new registration behavior that is specifically designed for a graduate student. (You learned how this is done in Chapter 5.)

This seems a sound decision, but suppose the programmer of a subclass forgets to redefine the registration behavior and uses the default behavior instead. The default registration behavior performs , but it registers the graduate student using the default registration process. The graduate student is registered, although improperly, because the programmer forgot to define the registration behavior for a graduate student. The default registration process may not address any idiosyncrasies required to register a graduate student.

Function vs. Functionality

Sometimes a behavior is common to multiple subclasses, but there isn t a default set of instructions to use to perform the behavior. For example, the registration process might be different for each category of student. This means the UndergradStudent class has a different registration process than the GradStudent class, but both classes must have a registration behavior (see Figure 6-2).

click to expand
Figure 6-2: Both subclasses have the same function but each has different functionality.

Programmers distinguish a behavior from instructions used to perform the behavior using the terms function and functionality . A function is a behavior. This is like the registration process. Functionality is the set of instructions used to perform the behavior. These are the steps used to register a student. Programmers make this distinction in order to implement abstraction.

The Power of Abstraction

Abstraction is the technique used by the programmer of a super class to require that programmers of its subclasses define the functionality of a behavior. Let s return to the example of the Student class and the GradStudent class to see how this works.

The programmer of the GradStudent class must redefine the registration function that is defined in the Student class because the registration function defined by the Student class is inappropriate for a graduate student.

There is the possibility that the programmer will forget to define a new registration function and use the default registration function instead. However, the programmer can be forced to define the registration function if the programmer of the super class makes the super class an abstract class.

An abstract class is a class that cannot be instantiated . That is, you cannot declare an instance of an abstract class. An abstract class must be inherited by a subclass in order for its data and behavior to be used in an application.

An abstract class can contain member methods (Java) or member functions (C++) that are designated by the programmer of the super class as abstract. This requires that the programmer of a subclass redefine the abstract method (function); otherwise , a compile error is displayed.

For example, the Student class can be designed as an abstract class and its registration method can be an abstract method. The programmer of the GradStudent class is forced to redefine the registration method in order to successfully compile the application (see Figure 6-3). You might say that abstraction is a way to remind a programmer of the missing method.

click to expand
Figure 6-3: Subclasses must define all abstract methods of the super class.

The Abstract Method

An abstract method does not require instructions because instructions are provided in the redefined method contained in the subclass. However, the programmer of the super class must define an empty abstract method as a member of the abstract class.

Some programmers look at this as specifying a required behavior (function) without defining how the behavior is performed (functionality). The subclass defines how the behavior is performed.

For example, the programmer of the Student class tells programmers of sub-classes that they must define a registration process by defining an abstract registration method in the super class. It is the job of the programmer of the GradStudent class to define instructions on how to register a graduate student.

In other words, the programmer of the super class specifies that there must be a registration function, and the programmer of the subclass defines its functionality.




OOP Demystified
OOP Demystified
ISBN: 0072253630
EAN: 2147483647
Year: 2006
Pages: 130

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