C5 OOP ObjectOrientedProgramming STUD FT
C5 OOP ObjectOrientedProgramming STUD FT
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.
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.
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.
2
Example:
#include <stdio.h>
int main() {
int n = 8; // number to find the factorial of
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.
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: 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.
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).
Advantages
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.
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.
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.
"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 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.
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.
Abstraction Encapsulation
11