You are on page 1of 11

Object Oriented Programming

Programming Paradigm:
Programming paradigm is used to specify an overall approach to writing program code.
The term programming paradigm refers to a style of programming. It does not refer to a
specific language, but rather it refers to the way programming is done.
Programming paradigms are different ways or styles in which a given program or
programming language can be organized. Each paradigm consists of certain structures,
features, and opinions about how common programming problems should be tackled.
A programming paradigm is a fundamental approach or style of programming that
provides a set of principles, concepts, and techniques for designing and implementing
computer programs. It defines the structure, organization, and flow of the code, as well as
the methodologies for problem-solving and expressing computations.
Each programming paradigm has its own set of concepts and features. For example,
procedural programming focuses on procedures and functions, object-oriented
programming revolves around objects and classes, functional programming emphasizes
immutability and pure functions, and so on. These paradigms provide guidelines and best
practices for organizing code, managing data, controlling program flow, and solving specific
types of problems.

Structured Programming:
Structure Programming is an imperative programming paradigm where we use nested
loops, conditional statements, and subroutines to define the control flow of a program. This
paradigm has a top-down implementation which promotes code readability and reusability. In
commercial applications, this helps to reduce development time and increase code quality.
Structured programming is a programming paradigm that allows the creation of programs
with readable code and reusable components and encourages dividing an application program into a
hierarchy of modules or autonomous elements.
Structure Programming makes an extensive use of control structures like loops (for, while),
conditionals (if, switch), and subroutines to control the program‟s flow. These structures enable
programmers to write code that is easier to understand and modify, because they provide a clear,
top-down control flow.
C is called structured programming language because a program in c language can be divided
into small logical functional modules or structures with the help of function procedure or function
module.
Sequence or block Structure: Execute a list of statements in order.
Lines or blocks of code are written and executed in sequential order.
Selection (if - else): Choose at most one action from several alternative conditions.
Execute a block of code (Action) if a condition is true. The block of code is executed at most once.
Repetition/Iteration: (while or for loop) Repeat a block of statements while a condition is true.
Repeat a block of code (Action) while a condition is true. There is no limit to the number of times that
the block can be executed
Examples: ALGOL, Ada, C, FORTRAN, Basic, Pascal etc.

The primary advantages of structured programming are:


a. It encourages top-down implementation, which improves both readability and
maintainability of code.

1
b. It promotes code reuse, since even internal modules can be extracted and made independent,
residents in libraries, described in directories and referenced by many other applications.
c. It's widely agreed that development time and code quality are improved through structured
programming.
d. Similar to English vocabulary of words and symbols.
e. It is user-friendly and easier to learn.
f. They require less time to write.
g. They are easier to maintain.
h. These are mainly problem oriented rather than machine based.
i. Program written in a higher level language can be translated into many machine languages
and therefore can run on any computer for which there exists an appropriate translator.
j. It is independent of machine on which it is used i.e. programs developed in high level
languages can be run on any computer.

The following are the disadvantages of structured programming:

a. Structured programs generally have redundant codes and are lengthy as compared to
unstructured programs.
b. Structured programs often use more memory and may execute slower.
c. Data types proceeds in many functions in a structured program. When changes occur in those
data types, the corresponding change must be made to every location that acts on those data
types within the program.
d. A high level language has to be translated into the machine language by translator and thus a
price in computer time is paid.
e. The object code generated by a translator might be inefficient compared to an equivalent
assembly language program.

Procedural programming
Procedural programming is a paradigm where the program is structured around procedures
or functions that manipulate data. It focuses on step-by-step instructions and emphasizes code
reusability through the use of functions.
Procedural Programming model or paradigm is based upon the concept of calling procedure,
also known as routines, subroutines or functions. During a program‟s execution, any given
procedure is called at any point, including by other procedures or itself.
Procedural Programming divides the program into procedures, also known as subroutines
or functions, simply containing a series of computational steps to be carried out when the procedure
is called. Each Procedure also known as a function, can have multiple commands to be
executed. The function, once defined, can be called as many times as needed to perform the
same operation.
Procedural programming is considered the best for beginners starting to learn to code.
Beginners can start with procedural programming as it is simple, efficient, requires less
memory, and is easier to keep track of control flow.

e.g.: FORTRAN, ALGOL, COBOL, BASIC, Pascal and C.

2
Example:

#include <stdio.h>

// Function to calculate factorial


int factorial(int n) {
int fact = 1;
for(int i = 1; i <= n; i++){
fact *= i;
}
return fact;
}

int main() {
int n = 8; // number to find the factorial of

// Call to the function factorial


int result = factorial(n);

printf("Factorial of %d is : %d\n", n, result);


return 0;
}

Key Features of Procedural Programming


Predefined funcitons, Local Variables, Global Variables, Modularity, Parameter passing

Advantages of Procedural Programming Language

a. Procedural Programming is excellent for general-purpose programming.


b. We can access the same code at different points in the software program without repeating it.
c. The memory need is also reduced by using the Procedural Programming technique.
d. The coded simplicity along with ease of implementation of compilers and interpreters.
e. The source code is portable, therefore, it can be used to target a different CPU as well.
f. The code can be reused in different parts of the program, without the need to copy it.
g. Through Procedural Programming technique, the memory requirement also slashes.
h. The program flow can be tracked easily.

Disadvantages of Procedural Programming Language


a.Difficult to relate to real-world objects.
b.We can not perform operations like encapsulation, inheritance, etc.
c.The program code is harder to write when Procedural Programming is employed.
d.The importance is given to the operation rather than the data, which might pose issues in
some data-sensitive cases.
e. The data is exposed to the whole program, making it not so much security friendly
A procedure can access and modify global data variables and store local data that is not accessible
from outside the procedure's scope.

Object-oriented programming (OOP)


Object-Oriented Programming or OOP (template, a conceptual framework) is a programming
paradigm in computer science that relies on the concept of classes and objects. It is used to structure a

3
software program into simple, reusable pieces of code blueprints (usually called classes), which are
used to create individual instances of objects.
OOP paradigm is based on the concept of objects which contain data (attributes or
properties) and code that is in the form of procedure (methods or functions or routines or
subroutines).
OOP is a computer programming design methodology that organizes or models software
design around data, or objects rather than functions and logic.
Examples of OOP languages : Java, C++, C#, Python, Objective-C, R, MATLAB etc.

Some of the features of OOP are:


a. Programs are divided into what are known as objects.
b. Class & Objects
c. Inheritance, Polymorphism
d. Data Abstraction & Encapsulation

Abstraction helps in hiding unnecessary attributes while showing the ones required.
Inheritance helps in establishing hierarchical relationships promoting code reusability.
Polymorphism allows different objects to respond in different ways to the same input.
Encapsulation is the binding of data into a single unit.

Class & Objects:


Classes and Objects are basic concepts of Object Oriented Programming that revolves around real life
entities. The objects in OOP are instances of classes which are known as the blueprint of objects.

Class: A class is a user defined (data type) blueprint, or template from which objects are created. It
represents the set of properties or methods that are common to all objects of one type.
A class, in object-oriented terminology, is a user defined (data type), or template for objects,
which consists of properties or attributes (data member) and behavior (methods/member function)
that are common to all objects of one type.
A class in OOP is used as a "type", with the objects being a "variable" of that type. Multiple
objects, or instances of a class can be created, multiple variables of the same type in any program are
declared.

Object:
An object is a real-world entity that has properties or attributes and behavior. Objects in an Object-
Oriented system represent real life entities such as a person, a place, an item, a device etc. It is a self –
contained unit or module of data and code in an OOP system.
Object is referred to as an instance. An object is referred to as a data field that has unique
attributes and behavior.
Object is an instance of a class in OOP Paradigm. All data members and member functions of
the class can be accessed with the help of objects.

Examples of Class and Objects

Class: Dry Fruits Object: Raisins, Almonds, Dates, Pistachios, Walnuts, Cashews, Peanuts, Fox
Nuts, Hazelnuts etc.
Class: Fruit Object: Apple, Banana, Mango, Pomegranate, custard apple, Cantaloupe, Papaya,
Guava etc.
Class: Vehicle Object: Car, Bus, Truck etc.
Class: Car Object: Audi, BMW, Cadillac, Mercedes, Porsche, Toyota etc.
Class: Bike Object: Ducati, Suzuki, Kawasaki, BMW, Honda, Harley, Triumph, Royal Enfield,
KTM etc.

4
Class: Mobile phone Object: iPhone, Samsung, Moto etc.
Class: Wild Animal Object: Lion, Cheetah, Hyena, Honey badger, Wildebeest etc.

From a programming point of view, an object can be a data structure, a variable, or a function
that has a memory location allocated. All Objects share the same copy of the member functions
(methods), but maintain a separate copy of the member data (Properties).
For example: A Ford car and a Toyota car are both Cars, so they can be classified as belonging
to the Car class. All have same movement (methods) but different in models (properties).

Procedural programming is about writing procedures or methods that perform operations on


the data, while object-oriented programming is about creating objects that contain both data and
methods. Object-oriented programming has several advantages over procedural programming:
a. OOP is faster and easier to execute
b. OOP provides a clear structure for the programs
c. OOP helps to keep the Java code DRY "Don't Repeat Yourself", and makes the code easier to
maintain, modify and debug
d. OOP makes it possible to create full reusable applications with less code and shorter
development time

What Is Object-Oriented Programming (OOP)

Advantages

Due to modularity and encapsulation, OOP offers ease of management


OOP mimics the real world, making it easier to understand
Since objects are whole within themselves, they are reusable in other programs
Disadvantages
Object-Oriented programs tend to be slower and use up a high amount of memory
Over-generalization
Programs built using this paradigm may take longer to be created

Procedural Programming vs Object-Oriented Programming:

Procedural Programming Object-Oriented Programming


Uses immutable data Uses mutable data
Follows the declarative programming model Follows the imperative programming model
Extends support to parallel programming Not suitable for parallel programming
The execution order of statements is not the primary focus . The execution order of statements is
very important.
Flow control is performed using function calls Flow control is performed through conditional
statements and loops.
Uses recursion concept to iterate collective data Uses loop concept to iterate collection data

Benefits of OOP include:

Modularity. Encapsulation enables objects to be self-contained, making troubleshooting and


collaborative development easier.
Reusability. Code can be reused through inheritance, meaning a team does not have to write the
same code multiple times.
Productivity. Programmers can construct new programs quicker through the use of multiple libraries
and reusable code.
Easily upgradable and scalable. Programmers can implement system functionalities independently.
5
Interface descriptions. Descriptions of external systems are simple, due to message passing
techniques that are used for objects communication.
Security. Using encapsulation and abstraction, complex code is hidden, software maintenance is
easier and internet protocols are protected.
Flexibility. Polymorphism enables a single function to adapt to the class it is placed in. Different
objects can also pass through the same interface.

a. OOP language allows to break the program into the bit-sized problems that can be solved easily
(one object at a time).
b. OOP systems can be easily upgraded from small to large systems.
c. The principle of data hiding helps the programmer to build secure programs which cannot be
invaded by the code in other parts of the program.
d. By using inheritance, we can eliminate redundant code and extend the use of existing classes.

Advantages of OOP

1. Re-usability
It means reusing an existing functionality rather than building them again and again. This is done
with the use of a class. We can use it „n‟ number of times as per our need.

2. Data Redundancy
This is a condition created at the place of data storage i.e. Databases, where the same piece of data is
held in two separate places. If a user wants a similar functionality in multiple classes, he/she can
write common class definitions for similar functionalities and inherit them.

3. Code Maintenance: This feature helps users from doing re-work in many ways. Maintaining and
modifying the existing codes by incorporating new changes into them is always easy and time-
saving. Changes and bug fixes can be made to specific objects or classes without affecting other parts
of the system, reducing errors and improving debugging.

4. Troubleshooting is easier with the OOP language


Suppose the user has no idea where the bug lies if there is an error within the code. Also, the user has
no idea where to look into the code to fix the error. This is quite difficult for standard programming
languages. However, when Object-Oriented Programming is applied, the user knows exactly where
to look into the code whenever there is an error. There is no need to check other code sections as the
error will show where the trouble lies.

5. Security
With a data hiding and abstraction mechanism, we filter out limited data to exposure, which means
we maintain security and provide necessary data to view.

6. Better Problem Solving: OOP models real-world systems, allowing developers to create
intuitive solutions that closely mimic real-world circumstances.

Disadvantages of an OOP
1. Learning Curve
Beginners who are new to OOP must learn advance concepts like encapsulation, inheritance and
abstraction. Neither of these concepts are present in previous programming languages. Moreover,

6
these programming concepts can be a bit complex to learn. OOPs take time to get used to it. The
thought process involved in object-oriented programming may not be natural for some people.
Everything is treated as object in OOP so before applying it we need to have excellent thinking in
terms of objects.

2. Increased complexity: OOP's emphasis on modularity and code organization can make larger
projects more challenging to understand and maintain. As the number of lines in code increases, the
programs becomes more complex as well. The developers need to put extra effort in creating OOP
programs.

3. Execution Speed
Since there are more lines of codes to be executed, the size of the program is also larger in OOP. This
eventually results in slower execution of the program.

4. Limited Performance: Since the OOP-based program takes larger space, the time required to
execute it will be increased, hence performance is reduced. The limited performance of OOP can also
make it harder to write code that meets certain performance requirements.

5. Overuse of inheritance: Improper use of inheritance can lead to complex class hierarchies and
tightly coupled classes, making code maintenance and modifications more difficult in the future.

Applications of an OOP

a. Real-Time System design: Real-time system inherits complexities and makes it difficult to build
them. OOP techniques make it easier to handle those complexities.
b. AI Expert System: These are computer application that is developed to solve complex problems
which are far beyond the human brain. OOP helps to develop such an AI expert System. It has the
some characteristics such as Reliable, Highly responsive, High-performance, Understandable etc.
c. Office automation System: These include formal as well as informal electronic systems that
primarily concerned with information sharing and communication to and from people inside and
outside the organization. OOP also help in making office automation principle.
Examples of office automation systems are Email, Word processing, Desktop publishing, Web
calendars
d. Neural networking and parallel programming: It addresses the problem of prediction and
approximation of complex-time varying systems. OOP simplifies the entire process by
simplifying the approximation and prediction ability of the network.
e. Stimulation and modeling system: Stimulating complex systems require modeling and
understanding interaction explicitly. OOP provides an appropriate approach for simplifying
these complex models.
f. Object-oriented database: The databases try to maintain a direct correspondence between the
real world and database object in order to let the object retain its identity and integrity.
g. Client-server system: Object-oriented client-server system provides the IT infrastructure creating
object-oriented sever internet (OCSI) applications.
h. CIM/CAD/CAM systems: OOP can also be used in manufacturing and designing applications as
it allows people to reduce the efforts involved. For instance, it can be used while designing
blueprints and flowcharts. So it makes it possible to produce these flowcharts and blueprint
accurately.
i. Hypertext and Hypermedia: Hypertext is similar to regular text as it can be stored, searched, and
edited easily. Hypermedia on the other hand is a superset of hypertext. OOP also helps in laying
the framework for hypertext and hypermedia.

7
Encapsulation:
The wrapping up of data and methods into a single unit (called class) is known as encapsulation.
Technically, in Encapsulation, the variables or data of a class are hidden from any other class and
can be accessed only through any member function of their class in which they are declared. In
encapsulation, the data in a class is hidden from other classes, which is similar to what datahiding
does. So, the terms “encapsulation” and “data-hiding” are used interchangeably. Encapsulation can
be achieved by declaring all the variables in a class as private and writing public methods in the class
to set and get the values of the variables.

Consider a real-life example of encapsulation, in a corporate company; there are different


sections like the accounts, finance, Sales section etc. The finance section handles all the financial
transactions and keeps records of all the data related to finance. Similarly, the Sales section handles
all the sales-related activities and keeps records of all the sales. Now there may arise a situation when
for some reason an official from the finance section needs some of the data about sales and
distribution in a particular month. In this case, it is not allowed to directly access the data of the sales
section. The official will first have to contact some other officer in the sales section and then request
him to give the particular data. This is what encapsulation is. Here the data of the sales section and
the employees that can manipulate them are wrapped under a single name “sales section”.

Inheritance:
Inheritance is an important pillar of OOP(Object-Oriented Programming). The capability of a class to
derive properties and characteristics from another class is called Inheritance.
Inheritance is the process by which objects of one class acquire the properties of objects of another
class. It supports the concept of hierarchical classification.
In OOP, the concept of inheritance provides the idea of reusability which means adding/enhancing
the feature to an existing class without modifying it.

When we write a class, we inherit properties from other classes. So when we create a class, we do not
need to write all the properties and functions again and again, as these can be inherited from another
class that possesses it. Inheritance allows the user to reuse the code whenever possible and reduce its
redundancy.
Superclass or Base Class: The class whose features are inherited is known as superclass (also
known parent class).
Derived Class: The class that inherits the other class is known as derived class (also known as
extended or child class).
Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create a new
class and there is already a class that includes some of the code that we want, we can derive our new
class from the existing class. By doing this, we are reusing the fields and methods of the existing
class. Java Support Single, Multilevel, Hierarchical Inheritance but not Multiple Inheritance with
classes.

8
Single Inheritance: One derived class inherits from one base class.
Multiple Inheritance: One derived class inherits from many base classes.
Multilevel Inheritance: One derived class inherits from other derived classes.
Hierarchal Inheritance: More than one derived classes inherit from one base class.
Hybrid Inheritance: A combination of more than one type of inheritance.

Single inheritance - Subclasses inherit characteristics from a single superclass.


Multiple inheritance - A subclass may have more than one superclass and inherit characteristics from
all of them.
Multilevel inheritance - A subclass may have its own subclasses. In other words, a subclass of a
superclass can itself be a superclass to other subclasses.
9
Hierarchical inheritance - A base class acts as the parent superclass to multiple levels of subclasses.
Hybrid inheritance - A combination of one or more of the other inheritance types.

In object-oriented programming (OOP), inheritance is a mechanism that allows a class to inherit


properties and behaviors from another class. It is a fundamental concept in OOP that promotes code
reuse and establishes relationships between classes.

"Inheritance is defined as a mechanism where the sub or child class inherits the properties and
characteristics of the super class or other derived classes. It also supports additional features of
extracting properties from the child class and using it into other derived classes."

Inheritance makes it possible to create a new child class that inherits the attributes and methods of
the original (parent) class. The term 'parent class' is also referred to as superclass or base class, while
'child classes' can also be referred to as subclasses or derived classes.

Inheritance can be used when we observe a 'kind-of' or 'is-a' relationship between classes. We can
say, for example, that "a dog is an animal" or "a dog is a kind of animal".
An inherited class is called a subclass or child class of the class it inherits from. And the class being
inherited is called either a parent class, superclass, or base class.

Polymorphism:
Encapsulation is a method of making a complex system easier to handle for end
users. The user need not worry about internal details and complexities of the system.
Encapsulation is a process of wrapping the data and the code, that operate on the data into
a single entity.
Polymorphism means the ability to take more than one form. It allows an object to have
different internal structures to share the same external interface. In simple words, we can define
polymorphism as the ability of a message to be displayed in more than one form. For example, A
geometrical figure at the same time can have different form/shapes like circle, square, triangle,
rectangle etc. Polymorphism is extensively used in implementing inheritance.

Abstraction.
Abstraction is an OOP concept that focuses only on relevant data of an object. It hides
the background details and emphasizes the essential data points for reducing the complexity
and increase efficiency. It generally retains only information which is most relevant for that
specific process. Abstraction method mainly focuses on the idea instead of actual
functioning.
Objects only reveal internal mechanisms that are relevant for the use of other objects, hiding
any unnecessary implementation code. The derived class can have its functionality extended. This
concept can help developers more easily make additional changes or additions over time.

Abstraction is hiding the details and implementation of the code.


Encapsulation is hiding the data and controlling the visibility of the code.
Abstraction is a design level process.
Encapsulation is an implementation level process.

Abstraction is a design level process and it is used to reduce the complexity at the designing stage of
a project.

10
Encapsulation is an implementation level process, and it is used to provide privacy and maintain
control over the transparency of data at the implementation stage of a project.

Firstly, we take an example of a banking application to understand Encapsulation and Abstraction.


Lets suppose we want to develop a banking application and gather all the details of the customer. We
gather some details that are irrelevant when developing a banking application. Hence, we have to
choose only valuable information like name, address, contact, tax info, etc. The process of fetching,
removing, and selecting the customer information from a pool of data is called Abstraction.

The information, once extracted, can be utilized for various applications. For example, we can use the
same data for job portal applications, hospital applications, a Government database, etc. with slight
or no amendment. Therefore, it works as the Master Data.

Let‟s take an example of mobile device. With the help of mobile devices, we can perform various
functions like taking a picture, sending a message, recording video/ audio, access the web and much
more.

Above mentioned features are functionalities of most of the smartphone. However, we don‟t need to
understand the internal functioning details of those features before using this program. For Example,
we don‟t need to know how our camera identifies a human face in an image or recognizes
fingerprints etc. We just need to learn the software interface. This is encapsulation.

Differences between Abstraction and Encapsulation

Abstraction Encapsulation

1. The process of hiding complex 1. The process of hiding the internal


implementation details and exposing details of an object or system and
only the essential features of an object exposing only the necessary
or system. information to the outside world.

2. Focuses on the external behavior and 2. Focuses on protecting the internal


functionality of an object or system. state and behavior of an object or
system.
3. We achieve it using abstract classes, 3. We implement it using classes,
interfaces, and inheritance. interfaces, and access modifiers such
as private, public, protected, etc.
4. It allows developers to create simpler, 4. It allows developers to create more
more modular code that is easier to secure, robust code that is less prone
understand and maintain. to bugs and errors.
5. Useful for creating generic code that can 5. Useful for creating objects and
be used in a variety of contexts. systems that can be used in complex
environments without compromising
their internal state or behavior.
6. Necessary for design patterns. 6. Enforces data integrity and prevents
unauthorized access to sensitive
information.

11

You might also like