Principles of Object Oriented Programming
Topics
1.1 Software Crisis
1.2 Software Evolution
1.3 POP (Procedure Oriented Programming)
1.4 OOP (Object Oriented Programming)
1.5 Basic concepts of OOP
1.5.1 Objects
1.5.2 Classes
1.5.3 Data Abstraction and Data Encapsulation
1.5.4 Inheritance
1.5.5 Polymorphism
1.5.6 Dynamic Binding
1.5.7 Message Passing
Software Crisis
• Developments in software technology continue to be dynamic.
• New tools and techniques are announced in quick succession.
• This has forced the software engineers and industry to continuously look for new
approaches to software design and development, and they are becoming more and
more critical in view of the increasing complexity of software systems as well as
the highly competitive nature of the industry.
• These rapid advances appear to have created a situation of crisis within the
industry.
The following issued need to be addressed to face the crisis:
• How to represent real-life entities of problems in system design?
• How to design system with open interfaces?
• How to ensure reusability and extensibility of modules?
• How to develop modules that are tolerant of any changes in future?
• How to improve software productivity and decrease software cost?
• How to improve the quality of software?
• How to manage time schedules?
Software Evolution
• Ernest Tello, A well known writer in the field of artificial intelligence, compared
the evolution of software technology to the growth of the tree.
• Like a tree, the software evolution has had distinct phases “layers” of growth.
These layers were building up one by one over the last five decades as shown in
fig., with each layer representing and improvement over the previous one.
• With the advent of languages such as c, structured programming became very
popular and was the main technique of the 1980’s.
• Structured programming was a powerful tool that enabled programmers to write
moderately complex programs fairly easily.
• However, as the programs grew larger, even the structured approach failed to
show the desired result in terms of bug-free, easy-to- maintain, and reusable
programs.
• Object Oriented Programming (OOP) is an approach to program organization
and development that attempts to eliminate some of the pitfalls of conventional
programming methods by incorporating the best of structured programming
features with several powerful new concepts.
• It is a new way of organizing and developing programs and has nothing to do with
any particular language.
• However, not all languages are suitable to implement the OOP concepts easily.
Procedure Oriented Programming (POP)
• In POP approach, the problem is viewed as a sequence of things to be done such as
reading, calculating and printing such as COBOL, FORTRAN, and C. The primary
focus is on functions.
• A typical structure for procedural programming is shown in fig. 1.2.
Relationship of data and functions in POP
Global Data Global Data
Function 1 Function 2 Function 3
Local Data Local Data Local Data
• In a multi-function program, many important data items are placed as global so
that they may be accessed by all the functions. Each function may have its own
local data.
• In a large program it is very difficult to identify what data is used by which
function.
• In case we need to revise an external data structure, we also need to revise all
functions that access the data. This provides an opportunity for bugs to creep in.
• Another serious drawback with the procedural approach is that we do not model
real world problems very well.
• This is because functions are action-oriented and do not really corresponding to
the element of the problem.
• Don’t have any proper way to hide the data, so it is less secure.
Some Characteristics exhibited by Procedure Oriented Programming are:
• Emphasis is on doing things (algorithms).
• Large programs are divided into smaller programs known as functions.
• Most of the functions share global data.
• Data move openly around the system from function to function.
• Functions transform data from one form to another.
• Employs top-down approach in program design.
Object Oriented Programming
• OOP treats data as a critical element in the program development and does not allow it to
flow freely around the system.
• It ties data more closely to the function that operate on it and protects it from accidental
modification from outside function.
• Program is divided in parts called objects.
Basic Concepts of Object Oriented Programming
• Objects
• Classes
• Data abstraction and encapsulation
• Inheritance
• Polymorphism
• Dynamic binding
• Message passing
Objects
• Objects are the basic run time entities in an Object Oriented System.
• They may represent a person, a place, a bank account, a table of data or any item that the
program has to handle.
• They may also represent user-defined data such as vectors, time and lists.
• Programming problem is analyzed in term of objects and the nature of communication
between them.
• Program objects should be chosen such that they match closely with the real-world objects.
• Objects take up space in the memory and have an associated address like a record in Pascal,
or a structure in c.
Classes
• We just mentioned that objects contain data, and code to manipulate that data.
• The entire set of data and code of an object can be made a user-defined data type
with the help of class.
• In fact, objects are variables of the type class.
• Once a class has been defined, we can create any number of objects belonging to
that class.
• A class is thus a collection of similar type objects.
• For examples, Mango, Apple and Orange members of class fruit.
• If fruit has been defined as a class, then the statement
Fruit Mango;
Will create an object mango belonging to the class fruit.
Data Encapsulation
• The wrapping up of data and function into a single unit (called class) is known as
encapsulation.
• Data encapsulation is the most striking feature of a class.
• The data is not accessible to the outside world, and only those functions which are
wrapped in the class can access it.
• These functions provide the interface between the object’s data and the program.
• This insulation of the data from direct access by the program is called data hiding or
information hiding.
Data Abstraction
• Abstraction refers to the act of representing essential features without including the
background details or explanation.
• Classes use the concept of abstraction and are defined as a list of abstract attributes such
as size, wait, and cost, and function operate on these attributes.
• They encapsulate all the essential properties of the object that are to be created.
• The attributes are some time called data members because they hold information.
• The functions that operate on these data are sometimes called methods or member
function.
Inheritance
• Inheritance is the process by which objects of one class acquired the properties of objects of
another classes.
• It supports the concept of hierarchical classification.
• For example, the bird, ‘robin’ is a part of class ‘flying bird’ which is again a part of the class
‘bird’. The principal behind this sort of division is that each derived class shares common
characteristics with the class from which it is derived as illustrated in fig. 1.6.
• In OOP, the concept of inheritance provides the idea of reusability.
• This means that we can add additional features to an existing class without modifying it.
• This is possible by deriving a new class from the existing one. The new class will have the
combined feature of both the classes.
• The real appeal and power of the inheritance mechanism is that it allows the programmer to
reuse a class.
Polymorphism
• Polymorphism is another important OOP concept. Polymorphism, a Greek term,
means the ability to take more than one form.
• An operation may exhibit different behavior in different instances. The behavior
depends upon the types of data used in the operation.
• For example, consider the operation of addition.
• For two numbers, the operation will generate a sum. If the operands are strings,
then the operation would produce a third string by concatenation. The process of
making an operator to exhibit different behaviors in different instances is known
as operator overloading.
Dynamic Binding (Late Binding)
• Binding refers to the linking of a procedure (function) call to the code to be
executed.
• Dynamic binding means that the code associated with a given procedure call is
not known until the time of the call at run time. It is associated with
polymorphism and inheritance.
• Consider the procedure “draw” in fig. 1.7. by inheritance, every object will have
this procedure.
• Its algorithm is, however, unique to each object and so the draw procedure will be
redefined in each class that defines the object.
• At run-time, the code matching the object under current reference will be called.
Message Passing
• An object-oriented program consists of a set of objects that communicate with
each other.
• The process of programming in an object-oriented language, involves the
following basic steps:
1. Creating classes that define object and their behavior,
2. Creating objects from class definitions, and
3. Establishing communication among objects.
• A Message for an object is a request for execution of a procedure and therefore will
invoke a function (procedure) in the receiving object that generates the desired
results.
• Message passing involves specifying the name of object, the name of the function
(message) and the information to be sent.
Example:
Some of the features of Object Oriented Programming are:
• Emphasis is on data rather than procedure.
• Programs are divided into what are known as objects.
• Data structures are designed such that they characterize the objects.
• Functions that operate on the data of an object are ties together in the data
structure.
• Data is hidden and cannot be accessed by external function.
• Objects may communicate with each other through function.
• New data and functions can be easily added whenever necessary.
• Follows bottom up approach in program design.
• Alan Kay coined the term “Object Oriented Programming” at grad school in 1966.
Object-based vs Object-Oriented Programming languages
• Object-based Programming is the style of programming that primarily supports
encapsulation and object identity.
• Major feature that are required for Object-based Programming are:
• Data encapsulation
• Data hiding and access mechanisms
• Automatic initialization and clear-up of objects
• Operator overloading
• Languages that support programming with objects are said to the Objects-based
Programming languages.
• They do not support inheritance and dynamic binding.
• Ada is a typical Object-based Programming language.
• Object-Oriented Programming Language incorporates all of Object-based
programming features along with two additional features, namely, inheritance and
dynamic binding.
• Object-Oriented Programming can therefore be characterized by the following
statements:
Object-based features + Inheritance + Dynamic Binding