ABSTRACT DATA TYPES
INTRODUCTION
Object-oriented Analysis and Design is a new way of approaching the solutions to problems related to the programming world. This concept is an evolutionary concept and offers a refreshing change from the obsolete programming concepts, such as modular programming (in modular programming, a program is divided into small units of code, called “modules”).In this new approach, models are organized around real world concepts. As we know that in the earlier programming concepts, the stress was upon solving the problem. The flaw in that approach was, no reusability of code was considered. All such flaws have been removed in the new approach – “Object-oriented Analysis and Design”.
In the OOAD, the central concept is the object, which constitutes the attributes (properties) and the behavior (operations). Any object can be thought of as an entity comprising of data structure and behavior. OOAD is useful for understanding the problems, modeling enterprises, designing programs and databases and last of all, preparing documentation.
In this chapter, first of all, we will have a brief overview of the modeling of real world through OOAD approach. Then, the various advantages directly accruing from the usage of OOAD such as autonomy, generation of correct applications and reusability will be discussed in detail. Then, we will get acquainted with the key features of OOAD i.e. classes, instance values, methods and messages. The various intricacies of objects and the operations associated with the generation and manipulation of objects and classes will also be discussed.
The term “Object-oriented” actually means that we organize the software as a collection of discrete and distinct objects that incorporate both data structure and behavior related to objects. This is contrasted with the approach followed in conventional programming techniques, in which the data structure and behavior are connected in a loose fashion. Object-oriented is an innovative approach of visualizing based on generalization and abstraction that exists in the real world.
MODEL OF REAL WORLD
In our real, physical world we deal with objects that are also real as are we are. For example in our world we have real tangible objects such as computers, books, furniture etc. These objects are not like data and they are not like functions either. Complex real world objects have both attributes and behavior.
Now what do we mean by attributes and behavior of objects? Let us consider some real object such as automobile. Every automobile has got certain features such as model (year of manufacture), color, price etc. These are the attributes of the object. Every automobile is also capable of performing some functions or operations. Such as it can be drove, it can be turned left or right, it can be purchased and so on. These operations that can be performed by the object are called functions or operations.
To further illustrate this point let us consider another example from our real world. Let us consider the book that you are currently reading. This book is also an object that can be felt, read. Its important attributes or properties are number of pages, price and author of the book. Now some important functions that can be performed by this book are to impart information to the user and to increase his awareness.
The object book encapsulates data, operations, other objects, constants and other related objects. Encapsulation means that all of this information is packaged under one name and can be reused as one specification or program component.
Thus summarizing, the object-oriented is:
Object-oriented = objects + classification + inheritance + communication
ATTRIBUTES
Attributes, as previously explained are the property of the class. Example of attributes (sometimes called characteristics) are, for people, skin color, eye color and job titles, and, for cars, model and number of doors. Attributes in the real world are equivalent to data in the programs; they have certain specific values, such as white (for ski color), black (eye color) or three (for the number of doors).
BEHAVIOR
Behavior is something a real world object does in response to some stimulus. If you ask your boss for a raise, he/she will generally say yes or no. If you apply the brakes in a car, it will generally stop. Saying something and stopping are examples of behavior. Behavior is like a function: you call a function to do something, like display the inventory; and it does it. Behavior is associated with every object. In other words we may also say that every object is capable of performing some functions or operations.
AUTONOMY
Object oriented programming approach offers several benefits to both programmer and the user. The biggest advantage offered is the autonomy. The autonomy means independence, freedom from the shackles and hold of previous procedural programming approaches.
This approach redresses many problems associated with previous approaches such as problem in the modeling of real world situations, no reuse of resources, poor memory management, and like. This technology offers several benefits such as improved programmer productivity, lesser maintenance costs and the foremost: better quality of software.
The salient features of this approach are: -
- The redundant code is eliminated and the existing classes can be extended through inheritence.
- Separate modules or functions can be reused from the existing classes rather than having to build the functions from the scratch.
- Encapsulation prevents the creeping of unknown bugs in the program by usage of same member’s names.
- Multiple instances of an object can be there without interference.
- By using the message passing technique for communication between objects, the interface with external objects becomes much more simpler.
- The scaling of program from small to large or vice-versa is facilitated.
- It becomes easy to work on a project through a group approach.
GENERATION OF CORRECT APPLICATIONS
The whole concept of Object-oriented analysis and design (OOAD) revolves around the facilitation of programming techniques in such a way that the whole process of coding results in generation of correct applications.Through OOAD approach, we expand our understanding of computer programming problems by providing an insight to the real world modeling through object-oriented approach. By following object-oriented approach, the whole programming cycle simplifies as a better understanding of problems results.
In object-oriented programming we generate an abstraction and then specify it in the form of classes, therefore any chances of unintentional errors creeping becomes very less. Moreover, due to other useful features offered by object-oriented programming such as polymorphism, inheritance and abstraction, the reusability of existing class members, such as data members and function members, is possible. All of the above factors accrue in generation of correct applications in a far efficient manner than offered by previous programming languages.
Reusability
The foremost advantage of object-oriented programming is the reusability feature. Through reusability, classes can be used with or without changes, depending upon the requirement.Now the question is what exactly is reusability. Once the class has been written, created, and debugged, it can be distributed to other programmers for use in their own programs. This is called reusability. It is similar to the way a library of functions in a procedural language can be incorporated into different programs. Through reusability feature of the object oriented analysis and design, a lot of effort and mundane work on the part of programmer is obviated.
In OOP, the concept of inheritance provides an important extension to the idea of reusability. A programmer can take an existing class and, without modifying it, add additional features and capabilities to it. This is done by deriving a new class from the existing one. The new class will inherit the capabilities of the old one, but is free to add new features of its own. Thus through inheritence the existing features of a class can be extended without affecting the base or parent class.
For example, you might have written a class that creates a menu system, such as that used in windows or other Graphic User Interface (GUIs). Now this class is working fine, and you don’t want to change it, but you want to enhance the capability to make some menu entries flash on and off. To do this, you simply create a new class that inherits all the capabilities of the existing one but adds flashing menu entries.
The ease with which existing software can be reused is an important benefit of OOP. Many companies find that being able to reuse classes on a second project provides an increased return on their original programming investment. Thus through reusability a lot of re-keying and developing existing classes from scratch is prevented.
Classes
The objects contain data and code to manipulate the data. The entire set of data and code of an object can be made user-defined data type with the help of a class. In fact, objects are instances of type class. Once a class has been defined, we can create any number of objects belonging to that class. Each object is associated with the data of type class with which they are created. A class is thus a collection of objects of similar type. For example, mango, apple, and orange are members of the class fruit. Classes are user defined data types and behave like the built in types of a programming language.
A class is an Object Oriented concept that encapsulates the data and procedural abstractions required to describe the content and behavior of some real world entity.
The data abstractions (attributes) that describe the class are enclosed by a wall of procedural abstractions (called operations, methods or services) that are capable of manipulating the data in some way. The only way to reach the attributes is to go through one of the methods that form the wall. Therefore, the class encapsulates data and the processing that manipulates the data. This achieves information hiding and reduces the impact of side effect associated with change. Since the methods tend to manipulate a limited number of attributes, they are cohesive; and because communication occurs only through the methods that make up the wall, the class tends to be decoupled from other elements of a system. All of these design characteristics lead to high quality software.
Stated another way, a class is a generalized description that describes a collection of similar objects. By definition, all objects that exist within the class inherit its attributes and the operations that are available to manipulate the attributes. A super-class is a collection of classes, and a subclass is a specialized instance of a class. A super-class can be taken as an abstraction of a number of similar classes, whereas a sub-class is a specialization of a particular class.
These definitions imply the existence of a class hierarchy in which the attributes and operations of the super-class are inherited by subclasses that may each add additional private attributes and methods.
INSTANCE VALUES
The instance’s dictionary meaning is example, illustration. By instance we mean an object and by instantiating a class, we use the class. Every class is a set of similar objects. So, by instantiating we actually make the objects of the class and use it in the program. By making the object corresponding to the class, we are modeling the real world. As in real world, we can generalize objects, similarly in the programming world; we first generalize the objects by making classes and then use it.
Every instance or more specifically an object has got certain characteristics. For example, a class called computer will has price, speed, and several other characteristics. But these characteristics will vary from one computer to another. Therefore, these characteristics have to be variable. Therefore in the computer world, we take some variable that represent those characteristics. These variables closely resemble the properties of the class that we are modeling. Now as the name suggests, the variable can take different values that are constrained by the data type of the particular variable. These values that the variable can take keeping in mind the constraints imposed by the data type chosen are called instance values.
For example, let us take a small class called pen. This class will have some characteristics and some functions that can be operated on these values. Some of these properties will be the color, price and make of the pen. Some of the operations associated with these properties can be setting the color, price and make of the pen and displaying the set values on the screen of the shop.
Methods and Messages
An object encapsulates data (represented as a collection of attributes) and the algorithms that process the data. These algorithms are called methods and can be viewed as modules in a conventional sense.
Each of the operations that are encapsulated by an object provides a representation of one of the behaviors of the object. For example, the operation GetColor for the object automobile has been designed to receive a message that requests the color of the particular instance of the class. Whenever an object receives a stimulus it initiates some behavior. This can be as simple as retrieving the color of an automobile or as complex as the initiation of a chain of stimuli that are passed among a variety of different objects. In the latter case, consider an example in which the initial stimulus received by the object 1 result in the generation of two other stimuli that are sent to the object 2 and object 3. Operations encapsulated by the second and the third objects act on the stimuli, returning necessary information to the first object. Object 1 then uses the returned information to satisfy the behavior demanded by the initial stimulus.
Messages are the means by which the objects interact. Using the terminology introduced in the preceding section, a message stimulates some behavior to occur in the receiving object. The behavior is accomplished when an operation is executed.
CREATING AND DESTROYING OBJECTS
The creation of objects involves the task of making copies of the member of class (attributes and methods) and putting them under a single unit object. Different OO programming languages use different syntax for creation of objects. The following quote is in fact true.
“Object oriented programming is not so much a coding technique as it is a code packaging technique, a way for code supplier to encapsulate functionality for delivery to customers”.
The creation of object also includes its INITIALIZATION, where in initial values are given to the attributes.
Figure (a)
While implementing objects using a programming language, objects are allocated memory such that the attributes of different objects are allocated different memory whereas methods of all objects share the memory.
Although basic data types such as int and float (in C++) are simply chunk of memory that hold a single value, some components of user-defined class require additional work during creation besides setting aside a number of bytes for storage. For example, the STRING class internally stores a pointer to an array of characters that hold the null-terminated string. A newly created ‘string’ object must set the pointer to a properly allocated array of characters and initialize that array to a specific string. This is important to facilitate statements like those given below:
string this_os = “Unix” ;
Special functions such as CONSTRUCTORS are used to initialize the object when it is created. Constructors have been dealt with explicitly under the heading CONSTRUCTORS AND DESTRUCTORS in the chapter “CLASS AND OBJECTS”.
On the other hand, there must be a way to release the memory when a string is no longer needed. This is facilitated by the use of special functions called DESTRUCTORS. Destructors de-allocate the memory used by an object.
CONSTRAINTS ON OBJECT AND INSTANCE VARIABLES
A class defines the operations that can be performed on an instance. A class defines the variables of the instances. A variable associated with a specific instance is called an instance variable. These variables are used to store the instance state.
Constraints are functional relationships between entities of an object model, where entity includes objects, classes, attributes, links and association. Constraints restrict the values that entity resumes for example, the age of a person cannot be less than or equal to zero. Constraints are expressed in a declarative manner. In a programming language, constraints are converted to procedural form.
PRE AND POST CONDITIONS OF METHODS
Pre and post conditions of methods are used in contractual model of programming. A precondition is a condition that the caller of an operation agrees to satisfy. A post condition is a condition that the operation itself agrees to achieve. An invariant is a condition that a class must satisfy at all stable times. Condition and invariants form a part of class declaration and must be obeyed by all descendent classes. Violating a condition or invariant at run time raises an exception, which either causes the faulty operation to fail or executes an exception handler for the class, if it has been included in the class.