next up previous print clean
Next: Notes and Comments Up: Object-orientation Previous: Introduction

Why object orientation?

Object orientation hinges on three fundamental concepts: encapsulation, inheritance, and abstraction.

Encapsulation (data-hiding). A single program unit, e.g. the array object above, unifies the data that represents the object's state (member variables) and the functions (member methods) that access or modify that state. To build such a program unit the programmer creates a class, a type description of the object not unlike a structure definition in C. An object is instantiated from a class by specifying its state. All objects of the same class have the same methods but different states. If classes compare to C structures, than a class's objects compare to variables of that structure type.

Inheritance (sharing common code). Classes are organized in hierarchies to share common member variables or methods. The class at the bottom inherits all members of the classes above and usually adds its own enhancements and specializations. Inheritance avoids unnecessary source code duplication, a vice any programmer avoids.

C++ and Java allow for classes that do not implement any of its members but only declare their name, arguments, and return types. Java calls such classes interfaces. Interfaces are important to set in stone the method names and arguments, so that programs can be written for any implementation of a given interface.

Abstraction (or polymorphism) is the idea to address objects of different classes as a common class type.

For example, an array of floats and an array of complex numbers are two distinct classes. However, if both classes share a common array class, than an application can use the common array methods to treat either array independent of its specific type. Such a single application can then process float arrays, complex-number arrays, or any other object of the array type. Usually, the common class is a class higher up in the class hierarchy. Expressiveness. Ease-of-use. Efficiency. run time method resolution (dynamic method resolution). strongly typed (requires casts). primitive types versus object types (simple things simple).

Object orientation. Encapsulating the data and its corresponding methods in a single programming unit, the class and its objects, allows us to isolate these programming units within objects of our application. For example, in the numerical optimization library Jest () we decided to isolate objects by expertise: numerical analysts are likely to write the solver classes while researchers in the natural science can concentrate on their particular operator. Vector classes are probably shared among researchers in the same community. Inherited method invocations for each class enforce that all objects collaborate with each other. Such an organisation follows the classic criteria of Parnas  of Carnegie-Mellon University to decompose a software system into modules so that any later change is likely to occur within a single module. Claerbout  shows a library of Fortran routines that fails to separate the physical science (operators) and the numerical analysis (solvers).

Malleability. Programs evolve and evolution is easiest if the changes are local. Good design will enclose all pertinent information within a single class. Access to the objects data is restricted to a few object methods. A change to the internal data does not effect any code outside classes as long as the class method's remain the same in functionality and invocation. The continuity of the member functions is often enforced by the object's inherited (possibly abstract) member functions. For example, an array class may change its internal storage of its elements from a fixed size array to a dynamic array with variable size. Applications that use the array class do not have to change if the array class methods remain the same.

Extensibility. Inheritance allows a programmer to incrementally add functionality by deriving new classes from the existing class hierarchy. These new classes are compatible with the existing class hierarchy since they inherit predefined member methods and implement more abstract parent classes.

These benefits usually rely on two or all three concepts of object orientation: encapsulation, inheritance, and abstraction. A pseudo-object-oriented language, such as Fortran90, that misses one of these concepts severly limits its usefulness.

In summary, On the scale set by Fortran and C, object-oriented languages such as C++ and Java stand far ahead in the level of discourse they allow.

 
say something about: 
run time method resolution (dynamic method resolution).
strongly typed (requires casts).
primitive types versus object types (simple things simple).



 
next up previous print clean
Next: Notes and Comments Up: Object-orientation Previous: Introduction
Stanford Exploration Project
3/8/1999