OOP Approach

Chapter 1 Object Oriented Programming Approach

The term Object Oriented programming is frequently heard in the programming arena. Object oriented approach was started to overcome the limitations of the earlier programming approaches. It is popularly known by its acronym OOP. It is used to develop reliable and reusable software.

The programming technology is continuously developing since the start of the computer and related technologies. New tools and techniques are included in programming in each phase of their development. Such enhancements increased complexity in programming and design of large software. Similarly, the users' requirements change and increase after the software is brought into operation. The software may need maintenance and enhancements after the regular feedback from the users. There could be problems to represent real life entities of problem while analyzing and designing system. While improving the software work may need to begin from the scratch that may increase software cost too. To incorporate users' demands and enhancements in software with such complex systems was difficult. To overcome such problems software developers were forced to develop new programming method. OOP was introduced to solve such programming problems.

1.1 Software Evolution

The software evolution occurred in several phases. Since the beginning of the first computer, programming for the computer started to develop software. The earlier electronic computer ENIAC was programmed in machine language by using switches to enter 1 and 0.

1.2 Basic of Object Oriented Programming

Objects are the entities that can be uniquely identified from others. They have their unique identity and found everywhere. In real world system everything exists in the form of objects. For example desk, bench, blackboard, student, teacher, car, tree are objects. Every object has two things, firstly its properties we call attributes and second its behavior we call function. For example a car is an object. It has attributes like color, number of seats, chassis number, engine number etc and behavior like move, stop, accelerate, turn etc. The Object Oriented Programming is developed to model such real world system. Its sole objective is to overcome the limitation of Procedure Oriented approach. Before discussing various features of Object Oriented Programming, it is wise to discuss characteristics of Procedure Programming and its limitations.

1.2.1 Procedure Oriented Programming

In procedure oriented programming a large program is broken down into smaller manageable parts called procedures or functions. In procedure oriented programming priority is given on function rather than data.

In procedure oriented programming language, a program basically consists of sequence of instructions each of which tells the computer to do something such as reading inputs from the user, doing necessary calculation, displaying output. When a program becomes larger, it is then broken into smaller units called procedure or functions. A number of functions are supposed to be written to accomplish such tasks. The primary focus of procedural oriented programming is on functions rather than data. These functions do not let code duplication. This technique is only suitable for medium sized software applications.

The procedure oriented programming can be diagrammatically represented as follows:

In procedure oriented programming two types of data local and global are used. Data within the function are called local data and the data which are not within any function are called global data. Global data are accessible to the only function where it is declared. So each function may access its local data as well as global data. The local data of one function is not accessible to other functions. If any data is to be accessed by two or more functions it should be made global. However, global data are vulnerable to another programmer to be changed unknowingly. Functions are action oriented and do not correspond to the element of the problem. The separate arrangement of data and functions does a poor job of modeling things in the real world. That’s why procedure oriented programming approach does not model real world system perfectly.

High Level Programming Languages like COBOL, FORTRAN, Pascal, C are common procedure oriented programming languages. Characteristics

The characteristics of procedure oriented programming are listed as follows:

  • A large program is broken down into small manageable procedures or functions.
  • Procedure oriented programming focuses on procedure or function rather than data.
  • For sharing a common data among different functions the data is made global.
  • Since global data are transferred from function to function; during the course of transformation the global data may be altered by the function.
  • The program design of procedure oriented programming follows top down methodology. Limitation

Even though procedure oriented programming approach is still used in software industry it has following limitations.

  • Focus on functions rather than data.
  • In large program, it is difficult to identify belonging of global data.
  • The use of global data is error prone and it could be an obstacle in code maintenance and enhancements.
  • The modification of global data requires the modification of those functions using it.
  • Maintaining and enhancing program code is still difficult because of global data.
  • It does not model real world problem very well. Since functions are action oriented and do not really correspond to the elements of problem.

1.2.2 Object oriented programming

The errors faced in the procedure oriented programming approach are the motivating factor in the invention of objected oriented approach. In OOP, data are treated as a critical element in the program and restricts freely transformation of data around the system. Instead, data are associated with functions that operate on it and protect it from accidental modification outside functions. OOP approach permits decomposition of a problem into entities called objects and then build data and function around them. Data of an object are accessible only by the function belonging with the object. But function of one object may access the function of another object.

Object-Oriented programming is a programming methodology that associates data structures with a set of operators which act upon it. In OOP, an instance of such an entity is known as object. In other words, OOP is a method of implementation in which programs are organized as co-operative collections of objects, each of which represents an instance of some class and whose classes are all members of a hierarchy of classes united through the property called inheritance. Characteristics

OOP is most sophisticated programming methodology among other methodologies by far. Some noticeable characteristics of OOP are as follows:

  • Emphasis is on data rather than procedures.
  • Programs are divided into objects.
  • Function and data are tied together in a single unit.
  • Data can be hidden to prevent from accidental alteration from other function or objects.
  • Data access is done through the visible functions so that communication between objects is possible.
  • Data structures are modeled as objects.
  • Follows Bottom up approach of program design methodology.

1.2.3 Procedure oriented versus Object oriented programming

The differences between procedural and Object oriented programming are tabulated below:

1.3 Features of Object Oriented programming

Different features of object oriented programming are explained here.

1.3.1 Object

Objects are the entities in an object oriented system through which we perceive the world around us. We naturally see our environment as being composed of things which have recognizable identities & behavior. The entities are then represented as objects in the program. They may represent a person, a place, a bank account, or any item that the program must handle. For example Automobiles are objects as they have size, weight, color etc as attributes (that is data) and starting, pressing the brake, turning the wheel, pressing accelerator pedal etc as operation (that is functions).


Following are some of the examples of objects in different scenario.

a) Physical Objects

· Bus in Traffic System

· Atom in chemical composition

· Diode in electronic system

· Humidity in metrological system

· Leader in political syste

b) Graphical User Interface

· Menu

· Button

· Toolbar

· Combo box

· Text box

· Windows

c) Data Structure

· Vector

· Stack

· Queue

· Tree

· Graph

d) Discipline of Human

· Actor

· Singer

· Musician

· Student

· Teacher

e) Geometrical Shapes

· Point

· Line

· Triangle

· Circle

· Ellipse

f) User defined data

· Distance

· Currency

· Time

· Date

· Complex Number

In computer programming, all these objects of the real world can be modeled by combining data and function together to make object of the program which is not possible in procedure oriented programming.

1.3.2 Class

Object consists of data and function tied together in a single unit. Functions are used to manipulate on the data. The entire construct of objects can be represented by a user defined data type in programming. The class is the user defined data type used to declare the objects. Actually objects are the variable of the user defined data type implemented as class. Once a class is defined, we can create any number of objects of its type. Each object that is created from the user defined type implemented as class is associated with the data type of that class. For example, manager, peon, secretary clerk are the objects of the class employee. Similarly, car, bus, jeep, truck are the objects of the class vehicle. Classes are user defined data type (like a struct in C programming language) and behave much like built in data type (like int, char, float) of programming language. It specifies what data and functions will be included in objects of that class. Defining class doesn�t create an object; however defining process specifies the data and function to be in the objects of its type.

One of the objects of student can have following values

Name = “Bishal”

Registration_number = 200876255

Marks = {66, 77, 51, 48, 82}

The function Sort_name () will sort and display list of students on the basis of name in alphabetical order. Similarly, function Tot_marks () will sum the marks obtained by the student. The function Percentage_marks() will calculate the percentage and the Decide_division () function will decide division based on percentage obtained by the student.

Each class describes a possibly infinite set of individual objects, each object is said to be an instance of its class and each instance of the class has its own value for each attribute but shares the attribute name and operations with other instances of the class. The following points give the idea of class:

  • A class is a template that specifies data and their operations.
  • A class is an abstraction of the real world entities with similar properties.
  • Ideally, the class is an implementation of abstract data type.

1.3.3 Abstraction

Abstraction is representing essential features of an object without including the background details or explanation. It focuses the outside view of an object, separating its essential behavior from its implementation.

We can manage complexity through abstraction. Let’s take an example of vehicle. It is constructed from thousands of parts. The abstraction allows the driver of the vehicle to drive without having detail knowledge of the complexity of the parts. The driver can drive the whole vehicle treating like a single object.

Similarly Operating System like Windows, UNIX provides abstraction to the user. The user can view his files and folders without knowing internal detail of Hard disk like the sector number, track number, cylinder number or head number. Operating System hides the truth about the disk hardware and presents a simple file-oriented interface.

The class is a construct in object oriented programming for creating user-defined data for abstraction. When data and it operation are presented together, the construct is call ADT (Abstract Data Type). In OOP classes are used in creating ADT. For example, a student class can be made and can be available to be used in programs. The programmer can implement the class in creating objects and its manipulation without knowing its implementation. The program can use the function Sort_name() to sort the names in alphabetical order without knowing whether the implementation uses bubble sort, merge sort, quick sort algorithms.

1.3.4 Encapsulation

The mechanism of wrapping up of data and function into a single unit is called encapsulation. Because of encapsulation data and its manipulating function can be kept together. We can assume encapsulation as a protective wrapper that prevents the data being accessed by other code defined outside the wrapper. By making use of encapsulation we can easily achieve abstraction.

The purpose of a class is to encapsulate complexity. Each data or function in a class can be marked as private or public. The public interface of a class represents everything that external users of the class may know about the data and function. The private function and data can only be accessed by code that is a member of a class. The code other than member of a class cannot access a private function or data. This insulation of data from direct access by the program is called data hiding. After hiding data by making them private, it then safe from accidental alteration.

The public interface should be carefully designed no to expose too much of the inner working of a class.

1.3.5 Inheritance

Inheritance is the process by which objects of one class acquire the characteristics of object of another class. We can use additional features to an existing class without modifying it. This is possible by deriving a new class (derived class) from the existing one (base class).This process of deriving a new class from the existing base class is called inheritance.

It provides the concept of hierarchical classification. It allows the extension and reuse of existing code without having to rewrite the existing code. We naturally view the whole world is made up of objects. Many objects are related to each other in a hierarchical way, such as vehicle, four wheeler, and car. If we describe vehicle in an abstract way, the attributes may be such as color, number of seats etc. All vehicles have common behavioral aspect like; they move, accelerate, turn and stop. The more specific class of vehicle is four wheeler that acquires all features of class vehicle and has more specific attributes like engine number, chases number etc. The class vehicle is called base class (or super class) and class four wheeler is called derives class (or subclass).

1.3.6 Reusability

Like library functions in procedural programming a class in Object Oriented Programming can be distributed for further use. In OOP, the concept of inheritance provides the idea of reusability. Once a class is completed and tested, it can be distributed for the development other programs too. The programmer can add new features or make some changes or can derive new classes from the existing class. This idea saves time and effort of a programmer. The testing of software will become easier as the already tested class should not be tested again. Suppose we have got a tested class Employee and we have to design a new class for Manager. The class Manager has all common features to class Employee. We can add some more features to class Manager using all features of class Employee.

If a software company creates generic classes for one project then the company can use the same class and its extensions in the new project with less time, effort and investment.

1.3.7 Polymorphism

Polymorphism means ‘having many forms’. The polymorphism allows different objects to respond to the same operation in different ways, the response being specific to the type of object. The different ways of using same function or operator depending on what they are operating on is called polymorphism.

Example of polymorphism in OOP is operator overloading, function overloading. Still another type of polymorphism exist which is achieved at run time also called dynamic binding.

For example operator symbol ‘+’ is used for arithmetic operation between two numbers, however by overloading (means given additional job) it can be used over Complex Object like currency that has Rs and Paisa as its attributes, complex number that has real part and imaginary part as attributes. By overloading same operator ‘+’ can be used for different purpose like concatenation of strings.

When same function name is used in defining different function to operate on different data (type or number of data) then this feature of polymorphism is function overloading.

1.3.8 Dynamic binding

The linking of a function call to the code to be executed in response to the call is called binding. There are two types of binding one is static binding( also called early binding) and another is dynamic binding( also called late binding). Function overloading and operator overloading construct in OOP are the examples of early binding. The early binding occurs at the compile time. This type of polymorphism occurring at compile time is called compile time polymorphism.

Dynamic binding means that the code associated with a given function call is not known until the time of the call at run time. It is achieved at run time so called as run time polymorphism. Dynamic binding is possible only when we use inheritance and access the objects through pointers. If classes Circle, Box, and Triangle are derived from same function draw(). During the function call draw() through the pointer variable an appropriate function belonging to that class is involved.

1.3.9 Message passing

Procedural programming languages have function driven communication. That is a function is invoked for a piece of data. Object oriented language have message driven communication. A message is sent to an object. Communications among the objects are analogous to exchanging messages among people.

An Object-Oriented program consists of set of objects that communicate with each other. Object communicates with each other by sending and receiving message (information). A message for an object is a request for execution of a procedure and therefore will invoke a function or procedure in receiving object that generate the desired result. Message passing involves specifying the name of the object name of the function (message) and the information (arguments to function) to be sent. In word, the message for an object is a request for the execution of a function belonging to an object which generates the desired result for the given argument.

student.fee (name) ;

object message information

Communication between the objects takes place as long as their existence. Objects are created and destroyed automatically whenever needed. In above example student is regarded as an object sending the message fee to find the fee to be paid by the student with the given name.

1.4 Popular Object oriented languages

An object-oriented programming language is one that follows object-oriented programming techniques such as encapsulation, inheritance, polymorphism. Simula (1967) is generally accepted as the first language to have the primary features of an object-oriented language. It was created for making simulation programs, in which objects were the most important information to be represented. In around 1972 to 1980, a pure object-oriented programming language Smalltalk was developed. It was called pure object oriented language because everything in them was treated as objects.

It was designed specifically to facilitate and enforce Object Oriented methods.

Some languages like Java, Python were designed mainly for Object Oriented programming along with some procedural elements.

Apart from this, some languages like C++, Perl are historically procedural languages, but have been extended with some Object Oriented features.

Besides these, there are some languages that support abstract data type but not all features of object-oriented programming. They are called object-based languages. Examples of such language are Modula-2, Pliant, Ada.

1.4.1 Smalltalk

Smalltalk is an object-oriented, dynamically typed, reflective programming language The development of Smalltalk language started in 1969 and it was publicly available in 1980. This language was developed by Alen Kay, Dan Ingalls, Adele, Goldberg at Xerox Palo Alto Research center (PARC). This language is 100% Object Oriented. The development of this language is influenced by language like Lisp, Simula, Logo, and Sketchpad. ANSI Smalltalk was ratified in 1998 and represents the standard version of Smalltalk.

In smalltalk, objects are called instance variables. All objects are dynamic. It offers fully automatic garbage collection and deallocation is performed by a built in garbage collector. All variables are untyped and can hold objects of any class. New objects are created using the same message passing mechanism used for operations on objects. All attributes are private to the class where as all operations are public. The syntax is very unusual and this leads to learning difficulties for programmers who are used to conventional language syntax.

Inheritance can be achieved by supplying the name of the super class. All attributes of super class are available to all its descendants. All methods can be overridden. Also multiple inheritance is not supported by standard implementation of Smalltalk. Rapid development of program is possible under its highly interactive environment.

Example Program of Smalltalk:

Transcript show: 'Hello, world!'

In the above code, the message 'show:' is sent to the object 'Transcript' with the String literal 'Hello, world!' as its argument. Invocation of the 'show:' method causes the characters of its argument (the String literal 'Hello, world!') to be displayed in the transcript ('terminal') window.

1.4.2 Eiffel

Eiffel was designed by a Bertrand Meyer in the late 1980's from France. It was developed by Bertrand and Eiffel Software. Eiffel is an ISO-standardized object-oriented programming language designed for extensibility, reusability, and reliability and programmer productivity. Eiffel is used in academic institutions as a language for teaching computer-programming principles. Eiffel is used in the finance, aerospace, health-care, video-gaming, and other industries as a development platform. Since 1985, many suppliers have developed Eiffel programming environments. The Eiffel language is influenced by Ada, Simula, Z etc.

In Eiffel, there is automatic memory management, typically implemented by garbage collection. The multiple inheritance is supported and there are mechanisms to make inheritance safe. Provides generic programming, constrained and unconstrained. In Eiffel every data type is based on class. It has keyword-based syntax as in the ALGOL/Pascal tradition but separator-free (semicolon is optional), operator syntax is available for routines. This language is case insensitivity and the syntax is very elegant and simple has fewer reserved keywords.

The compiler normally generates C source which is than compiled using C compiler which can lead long compile time. All Eiffel object are created on the heap storage. It is not a popular language for mainstream application development.

Example Program of Eiffel:








print ("Hello, world!")



1.4.3 Java

Java was designed by SUN (Stanford University Net) Microsystems, released in 1996 and is a pure object oriented language. The SUN says "Java is a new, simple, object oriented, distributed, portable, architecture natural, robust, secure, multi-threaded, interpreted, and high performance programming language".

It took 18 months to develop the first working version. Java was initially called "Oak". It was renamed Java in 1995. The objective of Java was "Write Once, Run Anywhere" (WORA). It was fairly secure and its security was configurable, allowing network and file access to be restricted. Major web browsers soon incorporated the ability to run secure Java applets within web pages. Java became popular quickly. With the advent of Java 2, new versions had multiple configurations built for different types of platforms. For example, J2EE was for enterprise applications and the greatly stripped down version J2ME was for mobile applications. J2SE was the designation for the Standard Edition. In 2006, for marketing purposes, new J2 versions were renamed Java EE, Java ME, and Java SE, respectively.

In 1997, Sun Microsystems approached the ISO/IEC JTC1 standards body and later the Ecma International to formalize Java, but it soon withdrew from the process. Java remains a de facto standard that is controlled through the Java Community Process. At one time, Sun made most of its Java implementations available without charge although they were proprietary software. Sun's revenue from Java was generated by the selling of licenses for specialized products such as the Java Enterprise System. Sun distinguishes between its Software Development Kit (SDK) and Runtime Environment (JRE) which is a subset of the SDK, the primary distinction being that in the JRE, the compiler, utility programs, and many necessary header files are not present.

On 13 November 2006, Sun released much of Java as free software under the terms of the GNU General Public License (GPL). On 8 May 2007 Sun finished the process, making all of Java core code open source, aside from a small portion of code to which Sun did not hold the copyright.

The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities. Java applications are typically compiled to bytecode which can run on any Java virtual machine (JVM) regardless of computer architecture.

One characteristic, platform independence, means that programs written in the Java language must run similarly on any supported hardware/operating-system platform. One should be able to write a program once, compile it once, and run it anywhere.This is achieved by most Java compilers by compiling the Java language code halfway (to Java bytecode) which means simplified machine instructions specific to the Java platform. The code is then run on a virtual machine (VM), a program written in native code on the host hardware that interprets and executes generic Java bytecode. Further, standardized libraries are provided to allow access to features of the host machines (such as graphics, threading and networking) in unified ways. Note that, although there is an explicit compiling stage, at some point, the Java bytecode is interpreted or converted to native machine code by the JIT ( Just In Time) compiler.

Example Program of Java:

// Hello.java

public class Hello {

public static void main(String[] args) {

System.out.println("Hello World!");



1.4.4 C#

C# is an object-oriented programming language developed by Microsoft as part of the .NET fromework and later approved as a standard by ECMA and ISO. Anders Hejlsberg leads development of the C# language, which has a procedural, object-oriented syntax based on C++ and includes aspects of several other programming languages most notably Delphi and Java with an emphasis on simplification. It was develop by Microsoft in 2000 and was standarderised by ECMA (European Computer Manufactor Association) in 2003.

Example Program of C#:

class ExampleClass


static void Main()


System.Console.WriteLine("Hello, world!");



1.4.5 D

The D programming language, also known simply as D, is an object-oriented, imperative, multiparadigm system programming language by Walter Bright of Digital Mars. It originated as a re-engineering of C++, but even though it is predominantly influenced by that language, it is not a variant of C++. D has redesigned some C++ features and has been influenced by concepts used in other programming languages, such as Java, C# and Eiffel. A stable version, 1.0, was released on January 2, 2007. An experimental version, 2.0, was released on June 17, 2007.

D is being designed with lessons learned from practical C++ usage rather than from a theoretical perspective. Even though it uses many C/C++ concepts it also discards some, and as such is not strictly backward compatible with C/C++ source code. It adds to the functionality of C++ by also implementing design by contract, unit testing, true modules, automatic memory management (garbage collection), first class arrays, associative arrays, dynamic arrays, array slicing, nested functions, inner classes, closures, anonymous functions, compile time function execution, lazy evaluation and has a reengineered template syntax. D retains C++'s ability to do low-level coding, and adds to it with support for an integrated inline assembler. C++ multiple inheritance is replaced by Java style single inheritance with interfaces and mixins. D's declaration, statement and expression syntax closely matches that of C++.

Example Program of D:

import std.stdio: writefln;

void main(string[] args) {

writefln(" Hello, World!");


1.4.6 C++

The programming language C++ was developed by Bjarne Stroustrup at Bell Lab in New Jersey in early 1980 s as extension of C. He named ‘C with Classes’. In 1983 it was renamed to C++. The operator ++ meaning that increment in C. ++ is increment operator in C/C++.Enhancements started with the addition of classes, followed by, among other features, virtual functions, operator overloading, multiple inheritance, templates, and exception handling. The C++ programming language standard was ratified in 1998 as ISO/IEC 14882:1998, the current version of which is the 2003 version, ISO/IEC 14882:2003. A new version of the standard known informally as C++0x is being developed

C++ is a general-purpose programming language. C++ is regarded as a mid-level language, as it comprises a combination of both high-level and low-level language features. It is a statically typed, free-form, multi-paradigm, usually compiled language supporting procedural programming, data abstraction, object-oriented programming, and generic programming. The C++ language corrects most of the deficiencies of C by offering improved compile time type checking and support for modular and object oriented programming. C++ supports multiple inheritance and does not have garbage collector dynamically created object must be destroyed explicitly.

Example Program of C++:

#include <iostream>

using namespace std;

int main()


cout<<’Hello, World!’);

return 0;


1.5 Advantages of OOP

Object oriented programming contributes greater programmer productivity, better quality of software and lesser maintenance cost. The main advantages are:

· Redundant code is eliminated by various techniques like inheritance templates.

· Through data hiding, programmer can build secure programs that cannot be invaded by code in other pats of the program.

· Existing classes can serve as library class for further enhancements. Classes are also available as library class in the standard library of the language.

· Because of division of program into objects makes software development easy.

· Software complexity is less severe than conventional programming techniques.

· Because of dynamic binding, addition of new classes of objects at run time is possible without modifying the existing code.

· The limitation realized in base class can be fulfilled in derived class without writing even a single piece of code in the base class.

· Upgrading and maintenance of software is easily manageable.

· System can be easily upgraded from small to large systems.

· Message passing technique makes the interface simpler with external systems.

· Models real world system perfectly.

· Code reusability is much easier than conventional programming languages.

1.6 Disadvantages of OOP

Compiler and runtime overhead. Object oriented program required greater processing overhead demands more resources.

  • An object's natural environment is in RAM as a dynamic entity but traditional data storage in files or databases
  • Re-orientation of software developer to object-oriented thinking.
  • Requires the mastery in software engineering and programming methodology.
  • Benefits only in long run while managing large software projects.
  • The message passing between many objects in a complex application can be difficult to trace & debug.