Chapter 5: Object Oriented Programming (OOP)
Object Oriented Programming (OOP)
·
OOP
is a programming model that uses "Object" to design application &
computer programs.
·
OOP
is a programming paradigm in which emphasis is given on data rather than
process.
·
OOP
allows decomposition of a problem into a number of entitles called objects and
then builds data functions around these objects.
·
C++,
Java, C#, Visual Basics are popular OOP languages.
Features
of Object Oriented Programming
•
Problems are divided into object.
•
Data hiding is possible.
•
It uses bottom up programming technique.
• Easy to add new data
Advantages of OOP
•
We can eliminate redundant codes by using inheritance
features of OOP.
•
It is very easy for managing complex and large size problems.
•
The more important is the reusability of codes by using the
features of inheritance.
•
It takes very less time for the development and maintaining
the software.
•
It is efficient for testing and implementation of the system.
•
It follows bottom up approach.
•
It can be implemented in the field of OODBMS (Object Oriented
Database Management System), OOAD (Object Oriented Analysis and Design) and
other different fields of engineering.
1. Complexity: OOP introduces complex concepts like
inheritance, polymorphism, and encapsulation, which can be difficult for
beginners to understand and implement correctly.
2. Slower Performance: Programs using OOP may run slower than
procedural ones because of features like dynamic dispatch, object creation, and
method calls that add overhead.
3. Larger Program Size: OOP programs often require more lines
of code and use more memory, as they include many classes and objects,
increasing the overall size of the application.
4. Overhead in Design: Designing an application using OOP requires more planning
and effort, especially in identifying the correct classes, relationships, and
hierarchies, which can slow down initial development.
Application of OOP
•
Image processing and pattern recognition
•
Computer aided design and manufacturing
•
Object Oriented Database Management System
•
Internet and Web based Applications
•
Mobile Computing
•
Data Warehouse and
Data Mining
• Digital Electronics
Object
•
An object is any entity, thing or organization that exists in
real world.
•
An object is a concept or thing which have data and
functions.
•
It consists of two fundamentals characteristics: its
attribute and behaviors.
•
For example: a dog is object which have attributes such as
color, weight, age, type etc. and behaviors such as barking, wagging tail etc.
In OOP, attributes are represented by data (variables) and the behaviors are
represented by the functions. An object can communicate with others by using
message passing mechanism.
·
A class is a blueprint for creating object.
·
A class is the collection of similar objects. In fact objects
are variables of type class. So, it is defined as the template or prototype
which contains the common attributes and behavior for all the objects of the
class.
·
Once a class has been defined, we can create any number of
objects associated with that class.
·
For example, mango, apple and orange are 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.
· There are three areas of class: public, private and protected. So, class incorporates the concept of data hiding. The functions and variables defined inside public area can be accessed by any object. The functions and variables defined inside private area can be accessed by the object of the same class and protected area can be accessed by the object from the same class and derived class.
The programming methods or approaches of program development
They are
1. Procedure Oriented Programming and
2. Object-Oriented Programming.
Procedure Oriented Programming is a conventional method of
programming and the Object-Oriented Programming is a modern or latest
programming method.
•
It is a conventional or old method of programming, in which
the program is written into many small parts and combined together.
•
In this approach, the functions are created and the data is
not very crucial.
•
Variables are created for the data handling and they are treated
as the global and local variables.
• Creation of the variables inside of the sub programs is known as local variable and the creation of the variables in the main module is called the global variable. Global variables can be accessed from any modules but the local variables can be accessed only within the local modules. The alteration of data is very high.
Features of Procedure Oriented Programming
·
A large program is broken down into small programs or
procedures.
·
It focuses on the functions rather than the data.
·
Variables are created as local and global.
·
The possibility of data alteration is very high, which is the
main disadvantage of this approach.
· It follows top down method.
•
A subtype of procedural programming using control structures.
•
Uses structured control flow (e.g., loops, conditionals).
• Similar to procedural but more disciplined in control flow.
Features |
Procedural
Programming |
Structured
Programming |
Object-Oriented
Programming (OOP) |
Definition |
A programming paradigm based on procedures or
routines. |
A subtype of procedural programming using control
structures. |
A paradigm based on objects which encapsulate data
and behavior. |
Program Structure |
Divides program into functions or procedures. |
Uses structured control flow (e.g., loops,
conditionals). |
Divides program into classes and objects. |
Data Handling |
Data is usually global or shared among procedures. |
Similar to procedural but more disciplined in
control flow. |
Data is encapsulated within objects; accessed via
methods. |
Code Reusability |
Limited reuse; functions can be reused with care. |
Slightly improved due to structure and modularity. |
High reusability through inheritance and
polymorphism. |
Example (Language)** |
C, BASIC |
Pascal, Structured C |
Java, C++, Python |
It
is a feature of hiding internal detail of any object. It provides only the
interface to the user, which makes them easy to use but does not show the
details of that object, how that works and how that is made. Due to this
feature, OOP has become very secure platform for its data from being accidental
alteration.
Encapsulation
It
is a process of combining the data and functions together. OOP gives more
emphasis on the data rather than the functions or procedures. Many functions
can use the same data but the instruction given to the function to use any
particular data and combining them together is the encapsulation. Due to its
unrelated functions cannot use unnecessary data in the program.
Inheritance
Inheritance
is the process of creating new classes based on the existing class. The new
classes require the features of the main class called the Super class and it is
provided through the feature called Inheritance. By the Inheritance feature
Super class can coordinate with it’s sub classes. It models the real world. It
allows the extension and reuse of existing code without having to rewrite for
the new created classes.
Polymorphism
It
is a feature of OOP, which refers to the way of operating the same operator in
different ways and different method or purpose. Operator overloading and the
operation overloading are the examples or Polymorphism. For example ‘+’
operator can be used for arithmetic operation and string concatenation both.
This facility or feature is an example of Polymorphism. It reduces the number
or keywords or operators.
Book's Acitvity Page number 249
1.
Multiple Choice
Questions
i. What is encapsulation
in object-oriented programming?
a A process of
converting objects into data
b. A way to hide the
implementation details and expose only necessary functionalities.
c.A technique to create
copies of objects.
d. A process of
organizing data into classes and objects.
ii.Which OOP principle
states that a subclass can inherit attributes and behaviors from a superclass?
a Polymorphism
b. Abstraction
c.Encapsulation
d. Inheritance
iii.Which term describes
the ability of an object to take on many forms?
a. Polymorphism
b. Encapsulation
c.Abstraction
d. Inheritance
iv. In object-oriented
programming, what is a constructor?
a. A method that is
called when an object is deleted.
b. A method used to
define the public interface of a class.
c. A method that is used
to access private data members of a class.
d. A special method used
to initialize objects and set their initial state.
v.What is the purpose of
the "super" keyword in many object-oriented programming languages?
a.It refers to the
current object being worked on. a
b. It is used to call
the superclass constructor.
c.It is a keyword
reserved for naming classes.
d.It is used to declare
static methods. d.
vi. What is the main
advantage of using inheritance in object-oriented programming?
a.It allows for the
creation of multiple instances of an object.
b. It ensures that
objects cannot be modified after they are created.
c.It allows new classes
to reuse properties and behaviors of existing classes.
d. It simplifies the
process of garbage collection.
viiWhich concept in OOP
allows a class to inherit properties and behaviors from multiple parent
classes?
a.Encapsulation
b. Overloading
c. Polymorphism
viii. What is a
"method overriding" in OOP?
a.The process of
creating a new method in a subclass with the same name as a method in the
superclass.
b.The process of
creating a copy of an existing method in a class.
c.The process of
creating a method that can take multiple forms.
d.The process of
defining a method in a subclass that has the same name and signature as a
method in the superclass.
ix. In OOP, what is a
"class"?
a A concrete instance of
an object.
b. A blueprint or
template for creating objects.
c. A method that is
inherited from a parent class.
d. A variable that holds
the data of an object.
2. Short Answer Questions
i.
List the advantage and disadvantage of structured programming approach.
Advantage:
·
Uses
structured control flow such as loops and conditionals, which makes the program
easy to read and debug.
·
More disciplined compared to unstructured
procedural programming, reducing logical errors.
Disadvantage:
·
Limited
code reusability.
·
Less
effective in managing complex and large programs compared to Object-Oriented
Programming.
ii.
What is inheritance? Write its types.
Inheritance is the process of creating new
classes based on an existing class. The existing class is called the
superclass, and the new class is called the subclass.
It allows extension and reuse of existing
code without rewriting.
Types:
1.
Single
Inheritance
2.
Multiple
Inheritance
3.
Multilevel
Inheritance
4.
Hierarchical
Inheritance
5.
Hybrid
Inheritance
iii.
What is polymorphism? Write its example.
Polymorphism is a feature of OOP that allows
the same operation or method to behave differently in different situations. It
reduces the number of operators or keywords used in a program.
Example: The '+' operator is used for both
arithmetic addition and string concatenation.
iv.
Write advantage and disadvantage of procedural programming.
Advantage:
·
Easy
to understand for small problems.
·
Structured
into functions, which makes the program modular.
Disadvantage:
·
High
possibility of data alteration due to use of global variables.
·
Less
secure and harder to manage large-scale programs.
·
Difficult
to reuse code effectively.
v.
What are the features of procedural programming?
·
A
large program is broken down into small parts or procedures.
·
Focuses
on functions or procedures rather than data.
·
Uses
local and global variables.
·
High
possibility of data alteration.
·
Follows
top-down programming approach.
3. Long Answer Questions
i.
Why is C++ called object-oriented programming language? Explain.
C++
is called an object-oriented programming language because it follows the
object-oriented programming paradigm, where the emphasis is on data rather than
procedures. It allows the decomposition of problems into entities called
objects, which encapsulate both data and functions. C++ supports key OOP
features such as:
Encapsulation: Combining data and functions
together.
Inheritance: Creating new classes based on
existing classes.
Polymorphism: Performing a single operation in
different ways.
Data Hiding and Abstraction: Hiding internal
details and exposing only necessary interfaces.
C++
also follows a bottom-up programming approach, making it efficient for managing
large and complex software development projects.
ii.
What are the differences between C and C++?
C |
C++
|
Procedural
programming language |
Object-oriented
programming language
|
Follows
top-down approach |
Follows
bottom-up approach
|
Focus
on functions and procedures |
Focus
on data and objects |
No
support for classes and objects |
Supports
classes and objects |
Limited
code reusability |
High
code reusability through inheritance and polymorphism |
iii.
What are the differences between procedural programming and object-oriented
programming?
Procedural
Programming |
Object-Oriented
Programming |
Program
is divided into functions |
Program
is divided into classes and objects
|
Focuses
on functions |
Focuses
on data
|
Uses
top-down approach |
Uses
bottom-up approach
|
Uses
global/local variables |
Data
is encapsulated in objects
|
Less
secure |
More
secure due to encapsulation
|
Limited
code reusability |
High
code reusability (Inheritance, Polymorphism) |
iv.
What is procedural programming? Explain its merits and demerits.
Procedural
programming is a conventional method where the program is divided into multiple
functions or procedures, and data is handled using global and local variables.
Merits:
·
Simpler
to implement for small problems.
·
Makes
use of modular structure with functions.
·
Efficient
for tasks that are linear and procedural in nature.
Demerits:
·
High
possibility of data alteration due to global variable use.
·
Less
secure for large applications.
·
Difficult
to manage and scale for complex programs.
·
Poor
code reusability compared to OOP.