Chapter 5: Object-Oriented Programming (OOP)
5.1 Programming paradigms: procedural, structural and object oriented
A
programming paradigm is a style or way of writing programs. It sets the rules
and methods for organizing and structuring code to solve problems. Some common
types of programming paradigms with real-world example are as follows:
A style or approach of
programming where the code is organized into procedures or functions that
perform specific tasks is known as Procedural Programming.
For example: Cooking a
meal using a recipe, where each step is clearly defined and followed in a
specific order.
A subset of procedural
programming that emphasizes dividing a program into small, manageable blocks or
modules to improve organization and readability is known as Structural
Programming.
For example: Building a
house with a blueprint, where each part of the house is planned and constructed
separately but contributes to the overall structure.
3) Object-Oriented Programming (OOP)/Event-Driven
(Object-Driven) Programming:
Object-Oriented Programming (OOP) is a programming paradigm based on the concept of objects, which contain both data (attributes) and methods (functions). OOP promotes modularity, reusability, and scalability by organizing code into classes and objects.
For example: Designing a
car, where each part (like the engine, wheels, and seats) is an object with its
own properties and functions, all working together to form the complete
vehicle.
5.2 Features (Characteristics) of OOP: Class, Object, Polymorphism and Inheritance
Class:
A class is a blueprint or
template for creating objects. It defines a set of properties (attributes) and
methods (functions) that the objects created from the class will have. The
class acts as a prototype from which individual objects are created,
encapsulating data for the object and methods to manipulate that data.
For example:
Imagine you have a
detailed blueprint for a car. This blueprint includes specifications like the
car's make, model, colour, engine type, and the number of doors. It also
outlines functions the car can perform, such as starting the engine,
accelerating, and braking. The blueprint itself is not a car but a guide for
manufacturing cars. All cars built using this blueprint will share these
specifications and functionalities.
Object:
An object is an instance
of a class. It is a specific realization of a class with actual values for the
properties defined by the class. Objects can interact with one another through
methods, making them the building blocks of an application. The object represents a real-world entity that combines both attributes(data) and behaviours (methods/functions).
For example:
An object is a specific
car manufactured using the blueprint. For example, a blue 2024 Tesla with a
hybrid engine, sunroof, and leather seats is an object of the Car class. This
specific car has actual values for the properties defined in the blueprint
(class) and can perform the functions (methods) outlined by the blueprint. Each
car (object) built from the blueprint can have different attributes (like colour
and features) but shares the general design and capabilities defined by the
class.
Encapsulation:
Encapsulation is a fundamental concept in Object-Oriented
Programming (OOP) that involves wrapping(bundling) data (attributes) and
methods (functions) into a single unit (class) and hides the internal details from the outside world.
Encapsulation plays a crucial role in data hiding by restricting direct access to the internal details of an object. In Object-Oriented Programming (OOP), encapsulation allows a class to control how its data is accessed and modified by:
1. Making Data Private: Class attributes (variables) can be declared as private so they cannot be accessed directly from outside the class.
2. Providing Controlled Access: Instead of allowing direct access to data, encapsulation provides getter and setter methods.
3. Hiding Internal Implementation Details: The internal workings of a class are hidden from external code, meaning users interact with the class through a controlled interface.
For example:
Think of encapsulation as
the dashboard of a car. While we interact with the dashboard to control various
functions (like speed, temperature, and fuel), we don’t need to know the inner
workings of the engine or other systems. The complexities are hidden from us.
Polymorphism:
Polymorphism refers the
ability of an object to take on different forms depending upon situations. It
allows objects of different classes to be treated as objects of a common
superclass. The same method(function) can behave differently on different
classes. Different types of Polymorphism
in OOP are as follows:
1) Compile-time Polymorphism
(Static Polymorphism)
2) Runtime Polymorphism
(Dynamic Polymorphism)
For example:
Consider a universal
remote control that can be used to operate different devices. The “start”
button might start the engine in a car, turn on the TV, or begin playback on a
DVD player. The same button (method) works in different ways depending on the
device (object).
a) Method Overriding: Subclasses can modify inherited methods for specific behaviour.
b) Method Overloading: Allows multiple functions with the same name but different parameters, improving readability.
c) Dynamic Binding (Runtime Polymorphism): Enables flexible and efficient function execution, supporting late binding.
d) Simplifies Code Maintenance: Reduces complexity by allowing generic function calls.
Abstraction:
Abstraction involves
hiding the complex implementation details and showing only the essential
features of the object.
For example:
Think of a car's gear
shift. When we shift gears, we don’t need to understand the complexities of the
transmission system. We just need to know how to use the gear shift to drive
the car.
Inheritance: In Object-Oriented Programming (OOP), inheritance is a fundamental concept that allows a
class (called a subclass or derived class) to inherit properties and behaviours
(methods and attributes) from another class (called a superclass or base
class).
For example:
Superclass = Animal (base
class)
Subclass = Mammal, Bird,
Fish (derived classes)
Inheritance = Mammals,
Birds, and Fish inherit common features (like eat()) from the Animal class, but
they also add their own specific features (like nurse() for Mammals, fly() for
Birds, swim() for Fish).
Different types of
inheritance in Object-Oriented Programming (OOP) are as follows:
1) Single Inheritance:
A subclass inherits from
one superclass.
Example: Dog inherits from
Animal.
2) Multiple Inheritance:
A subclass inherits from
more than one superclass.
Example: Dog inherits from
Mammal and Canine.
3) Multilevel Inheritance:
A subclass inherits from
another subclass, forming a chain.
Example: Puppy inherits
from Dog, and Dog inherits from Animal.
4) Hierarchical Inheritance:
Multiple subclasses
inherit from a single superclass.
Example: Dog and Cat both
inherit from Animal.
a) Code Reusability: Common functionalities are defined in a base class and reused in derived classes.
b) Faster Development: New classes can inherit existing logic instead of writing from scratch.
c) Scalability: Easy to extend existing code with additional features.
d) Better Organization: Establishes a clear relationship between general and specific classes.
5.3
Advantages of OOP
1) Modularity:
OOP allows you to divide
your program into smaller, self-contained units (classes), making it easier to
manage and debug.
2) Code Reusability:
Through inheritance, OOP
promotes code reuse. Once a class is written, it can be reused by other classes
without modification.
3)Maintainability:
OOP's modular structure
makes it easier to update and maintain. Changes in one class can be made
without affecting others.
4)Abstraction:
OOP helps hide complex
implementation details and exposes only essential features, making it easier to
understand and use objects.
5)Encapsulation:
OOP allows data and
methods to be bundled together in classes, protecting data from unintended
access or modification, leading to better data integrity.
6)Flexibility &
Scalability:
OOP supports polymorphism
and inheritance, allowing the system to evolve easily by adding new classes or
modifying existing ones.
7)Security:
OOP supports data hiding
through access modifiers (private, public, protected), which helps in
controlling how data is accessed and modified, providing an additional layer of
security.
1. OOP can be harder to learn and understand, especially
for beginners, due to its advanced concepts.
2. Programs written in OOP can be slower and use more
memory compared to procedural programming.
3. OOP often results in larger codebases because of the
multiple classes and objects.
4. Designing OOP-based applications can take more time
due to the need for careful planning and structuring.
5. For simple or small programs, OOP might be overkill, making procedural programming a more practical choice.
5.4 Application
of OOP
Object-Oriented
Programming (OOP) is widely used across many domains and applications due to
its advantages like modularity, reusability, and ease of maintenance. Here are
some common applications of OOP:
1. Graphical User
Interface (GUI) Applications
Examples: Java Swing, C#
Windows Forms.
2. Video Games
Examples: Unity (C#),
Unreal Engine (C++).
3. Web Development
Backend Examples: Laravel
(PHP).
Frontend Examples: React
(JavaScript).
4. Enterprise Applications
Examples: ERP (Enterprise
Resource Planning), CRM (Customer Relationship Management) systems.
5. Simulation Systems
Examples: Traffic
simulation, flight simulation software.
6. Artificial Intelligence
& Machine Learning
Examples: TensorFlow,
scikit-learn (Python).
7. Operating Systems
Examples: Windows (parts
using C++), macOS (Objective-C, Swift).
8. Embedded Systems
Examples: Robotics
software, IoT applications.
9. Cloud Computing
Examples: AWS, Microsoft
Azure.
10. Mobile Applications
Examples: Android
(Java/Kotlin), iOS (Swift/Objective-C).
Imp
Question) Compare the OOPs and procedural programming language.
Aspect |
OOP (Object-Oriented Programming) |
Procedural Programming (Structural
Programming) |
1. Paradigm |
Focuses on objects and classes. |
Focuses on functions and procedures. |
2. Structure |
Organized around objects that encapsulate data and behaviour. |
Organized around a sequence of tasks or steps. |
3. Data Handling |
Emphasizes data encapsulation and security. |
Data is often exposed and shared globally. |
4. Code Reusability |
Achieved through inheritance and polymorphism. |
Reusability is limited to function calls. |
5. Modularity |
Programs are divided into objects and classes. |
Programs are divided into functions. |
6. Scalability |
Better suited for large and complex systems. |
Less suitable for complex, large systems. |
7. Development Approach |
Follows a bottom-up approach, building reusable objects first. |
Follows a top-down approach, breaking the task into smaller
procedures. |
8. Examples of Usage |
Used for GUI, simulations, and real-world modeling. |
Used for smaller, process-driven tasks. |
9. Examples of Languages |
Java, C++, Python , C#. |
C, Pascal, Fortran, Basic. |
10. Polymorphism and Inheritance |
Supports both for flexibility and code reuse. |
Not supported natively. |
11. Debugging |
Easier to debug and maintain due to modularity. |
Harder to debug in large systems. |
(ASMITA'S Question Bank)
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home