|
|
||
![]() |
Department of
Mathematics and Computer Science |
|
Some considerations:
-- Fortran had only local variables (now MODULES have been introduced).The result was the creation of a way to encapsulate a large structure with its own special operations (e.g., a stack, with Push and Pop) and its own local variables as part of another language object. In the late 1970s and 1980s this was incarnated in Modula-II (successor to Pascal) as modules, in Ada (DOD Language) as packages, in Path-Pascal (developed at the Univ of Illinois) as objects.
-- Pascal had global variables (and so does C), but too many global variable become unwieldy. Separate compilation in some languages is not standard.
-- Older languages could not immediately handle the possibility of parallel processors, i.e., different processors working on the same program at the same time.
NOTE: Path-Pascal also include the concept of a "path statement" which could specified the order in which local functions could be invoked and the maximum number of copies of the structure that could be active at any one time.
EXAMPLES
ADA
PACKAGE REALSTACK IS
TYPE STACK IS PRIVATE
PROCEDURE PUSH(F:IN FLOAT; S:IN OUT STACK);
PROCEDURE POP(F:OUT FLOAT; S:IN OUT STACK);
END REALSTACK;
Advantages to the use of Data Encapsulation
Typical programs need to share program objects (such as data, functions, etc.) between several program segments. One can use global variables, but this can be dangerous. One can pass parameters, but this is often inefficient and cumbersome for complex, large, and numerous data.
We would like to be able to share variables as if we were using globals but without uncontrolled/unauthorized access,.
We also would like to include the ability to
-- restrict the name space (i.e., "hiding") from the rest of the code,Basically, we don't want unexpected sharing of data and code, but we want to preserve the benefits of global variables.
-- make the lifetimes of variables dependant on the lifetimes of the entire code, if desired,
-- include other aspects of "encapsulation."
This is a crucial concern for software engineering projects, especially those with many programmers working together on a large project.
This had led to various features in recent languages, such as MODULES in Fortran-90, or packages in Ada. Such units can store data and functions/procedures/subroutines and the units can be accessed on a limited basis via a use or with statement. In some languages, the contents can be restricted with public or private labels.
The concern about data encapsulation led to developing a new paradigm for programming languages, data structures, and the way one designs a program.
(Cf. article by P. Marquess in Dec Professional, March 1991.)
One definition of OOP is the following:
Objects + Classes + Inheritance = Object-Oriented
As an illustration, let us consider the common data structure of a stack. In older (non-OOP) languages, one would create the data storage structure (array or linked list) to store the information in the stack separately from the operations of Push, Pop, Peek used to interact and change the information. Thus, we might have:
Information Operations
Array/Linked List Push
Top of Stack Ptr Pop
Peek
In this model, it is easy to access the information directly and bypass
the operations associated with the structure. In this model, information (data)
and operations are separate and only linked by the way a programmer
codes a program.
In an OOP Language, one would normally encapsulate the information and declare it PRIVATE, allowing access to it only via the related member operations. Thus, we might have:
CLASS: Stack
Public:
Push (operation)
has access to Array/Linked List
and Top of Stack Ptr (both private)
Pop (operation)
has access to Array/Linked List
and Top of Stack Ptr (both private)
Peek (operation)
has access to Array/Linked List
and Top of Stack Ptr (both private)
[Private:
Array/Linked List, Top of Stack Ptr ]
part of Class, but not accessible
outside of Class
One problem is that OOP requires a new approach to programming. One is concerned with objects and what they are (which is usually more than just data), rather than procedures and what they do.
OOP requires a different approach to thinking about programs than with classical procedural languages. OOP also presents programmers with new problems and challenges!
For example, many people, accustomed to writing with GOTO's in BASIC or FORTRAN, initially try to use GOTO's in Pascal and C, and ignored the structures (e.g., case/switch, while, repeat, do, for) the new languages contain. Merely using Pascal or C doesn't automatically lead to good, structured code.
Some purists insist that the only real OOP language is Smalltalk. Thus one can be fooled into thinking one understands OOP when using C++ or Java, since these languages are based on C, a procedural language.
Some say that one can understand Java better if one begins with Smalltalk and introduces the syntax of C rather than starting with C and introduces some OOP concepts. With Smalltalk, everything is an object and communicates with other objects via methods (i.e., member functions). This is not the way one normally thinks of C/C++. Older languages tend to separate data from functions. OOP languages start with an OBJECT which contains both information (data) and activity (functions) united together.
Others argue that many types of programming problems are not suited to the OOP paradigm and that forcing such a paradigm on, for example, a solution of a matrix-vector equation, may actually complicate matters considerably.
The best approach is to use a language which is appropriate to a problem in a way that uses the best features of a language and makes the solution of a problem as clear as possible.
This page is maintained by Dennis C. Smolarski, S.J.
dsmolarski at scu.edu
Last updated: 15 October 2007.