Object Oriented Programming (OOP)
Oriented Programming: An
Published in CORE, Jan/Feb, 1994
Object oriented programming is not just another buzz word of software developers. Object-oriented programming is a relatively new method for designing and implementing software systems. We see them in literature, we hear about them and many of us actually use them without knowing what object oriented software really is. Nowadays softwares based on this concept is pretty ubiquitous, Microsoft's Windows is one of them. Its major goals are to improve programmer productivity by increasing software extensibility and reusability and to control the complexity and cost of software maintenance. It rewards the development of generic functions which can be used with different data types e.g. integer, real etc..
The conventional method of programming using languages as Pascal, basic, C etc. is called imperative or declarative programming. A fundamental flaw of imperative programming is that global variables can potentially be accessed (and updated) by every part of the program. Large programs that lack any discipline tend to be unmanageable. The reason for this is that no module (a group of program codes) that accesses a global variable can be developed and understood independent of other modules that also access that global variable.
Object-oriented programming enables us to create software that can be readily comprehended and shared with others. Unlike more traditional programming methods that are based on concepts such as data flow or mathematical logic, object-oriented programming directly models the application.
There are as many different views of what object-oriented programming is as there are computer scientists and programmers. But basically a language must have following four elements to support object-oriented programming :
What is an object
An object is a logical entity that contains both data and code that manipulates that data within an object, some of the code and/or data may be private to the object and inaccessible to anything outside the object. Thus an object is a variable equipped with operations that have the exclusive rights to access it. Programs perform computations by passing messages between active objects, which are computer analogy of entities in the real world. For example an object oriented financial application might see Customer objects sending debit and credit messages to Accounts objects.
Objects can be reused in similar applications in the same area. Reusing, rather than reinventing software speeds the development and maintenance of large applications.
What is a class
Object and class are fundamental concepts of object oriented programming. An object class is a set of objects that share the same operations. In most object-oriented languages, a class definition describes the behavior of the underlying abstract data type by defining the interface to all the operations that can be performed on the underlying type.
Each class definition also specifies the implementation details or data structure of the type. The data structure which are accessible only within the scope of the class is called a private type. And the one which are accessible outside the class are called public.
The operations that are defined on the type are also generally classified as either public or private. The public operations are those that are accessible outside the scope of the class. The private operations are accessible only within the scope of the class.
Methods and message Passing
In object-oriented programming, the operations that are defined for a class are called methods. These methods are analogous to procedures and functions in non-object oriented languages.
Actions may be performed on an object by invoking one or more of the methods defined in the class definition. The process of invoking a method is called sending a message to the object. Such a message typically contains parameters just as in a procedure or functions call invocation in a non-object-oriented languages. The invocation of a method (sending a message to an object) typically modifies the data stored in the particular object.
Encapsulation is the technical name for information hiding. An object in object oriented programming allows some part of its code and/data to be inaccessible outside the object. In this way, an object provides a significant of protection against some other, unrelated part of the program accidentally modifying or incorrectly using the private parts of the object. This linkage of code and data is often referred to as encapsulation.
Instead of organizing programs into procedures that share global data, the data is packaged with the procedures that access data. This concept is often called data abstraction or modular programming. The goal here is to separate the user of the object from its implementer. The user is no longer aware of how the object is implemented. Users can only operate on an object using those messages that the implementer provides. This has the obvious benefit that you can change the implementation of the encapsulated object without affection the applications using it.
Inheritance is the major feature distinguishing an OOPS from other programming systems.Every OOPS provides simple inheritance in one form or another. Inheritance increases code sharing by allowing the language rather than the programmer to reuse code form one class in another related class.
Inheritance enables programmers to create classes and, therefore, objects that are specializations of other objects. For example, we might create an object, vehicle, that is a specialization of a car, which is a specialization of sports car. A sports car inherits behavior that is appropriate for car, and vehicle.
Creating a specialization of an existing class is called subclass.The new class is a subclass of the existing class, and the existing class is the superclass of the new class. The subclass inherits instance variables,class variables and methods from its superclass. The subclass may add instance variables, class variables and methods that are appropriate to more specialized objects.
Late binding and Polymorphism
It is quite common in object-oriented systems to code multiple classes of an object that respond to the same messages. The ability of different objects to respond differently to the same message is known as polymorphism. This approach is clearly superior to using a huge case statement for all the known graphical data types in a single global draw procedure. Polymorphism is partly responsible for a well-known characteristic of object-oriented systems, a style of programming sometimes referred to as differential programming or programming by modifications.
At the design level, when a software engineer is deciding what type of action is appropriate for a given object, he or she should not be concerned about how the object interprets the action (message) and implements the methods but only what the effect of the action is on the object. Object-oriented languages like C++ and Smalltalk allow a programmer to send identical messages to dissimilar but related objects and achieve identical actions while letting the software system decide how to achieve the required action for the given object. A key issue associated with polymorphism is the timing of the software system's implementations decision. If the system decides how to implement an action at compile time, this is called early binding. If the decision is made dynamically at run-time,it is called late biding. Generally late biding offers the advantages of flexibility and a high level of problem abstraction.
C++ and Object-Oriented Programming
C++ is based on C and is a superset of C. C++ retains C's power and flexibility to deal with the hardware-software interface and low-level systems programming. The efficiency, economy, and power of expression of C are retained in C++. More importantly, however, C++ provides a platform to support object-oriented programming and high-level problem abstraction. C++ goes further than ADA in its support for object-oriented programming and is similar to MODULAR-2 in its simplicity and support for modularity,while retaining the cutting-edge efficiency and compactness of C.
C++ is a hybrid language. The object-oriented paradigm can be exploited fully to produce a purely object-oriented solution to a problem, or C++ can be treated as a procedural language that contains some additional constructs beyond C. In practice, C++ software reflects both the procedural programming paradigm as well as the newer object-oriented paradigm. This duality in C++ presents a special challenge to the beginning C++ programmer. Not only is there a new language to learn, but there is also a new way of problem solving and thinking.
C++ was developed by Bjarne Stroustrupat Bell Labs in the early 1980s. C++ is designed to support software development on a large scale. The most significant aspect of the C++ language is its support for the object-oriented programming .
Classes are what make C++ suitable for developing object-oriented programs. The class structure meets the requirements for creating objects. However, object-oriented programming is also supported by C++'s strong typing, operator overloading, and less reliance on the preprocessor. In a more formal definition, a class defines the properties and attributes that depict the actions of an object that is an instance of that class.
Object-Oriented Design Methods
At the current stage of evolution, OOD methodology combines elements of all three design categories : data design, architectural design, and procedural design. By identifying objects, data abstractions are created. By defining operations, modules are specified and a structure for the software is established.
The steps for OOD are as follows :
Although object oriented system design is the design method for the future, its effect can be hardly felt on smaller programs (less than 3000 lines). For large applications like Windows you know the effect. You might have noticed that in Windows every pop up menu have similar options e.g. move, size. etc., this means the code is being reused many times which reduces the program length and execution time. And that's what object oriented programming is all about.