- Learning Objectives
- Prerequisites
- Introduction
- Programming Paradigms
- What is Procedural Programming?
- Limitations of Procedural Programming
- Why Object-Oriented Programming?
- What is Object-Oriented Programming?
- Real-World Analogy
- Features of OOP
- Four Pillars of OOP
- Advantages of OOP
- Summary
After completing this chapter, you will be able to:
- Understand Object-Oriented Programming.
- Explain why OOP was introduced.
- Differentiate Procedural Programming and OOP.
- Understand the basic principles of OOP.
- Identify real-world examples of OOP.
- Understand the four pillars of OOP.
Before learning this chapter, you should know:
- Variables
- Data Types
- Functions
- Control Statements
- Operators
As software applications become larger and more complex, writing programs using only functions becomes difficult.
Consider applications like:
- Banking System
- Hospital Management System
- Railway Reservation System
- E-Commerce Website
- Social Media Applications
These applications contain thousands or even millions of lines of code.
Managing such large applications using only functions becomes difficult because:
- Code becomes lengthy.
- Code duplication increases.
- Maintenance becomes difficult.
- Debugging becomes time-consuming.
- Reusability becomes limited.
To overcome these problems, Object-Oriented Programming (OOP) was introduced.
OOP organizes programs around objects rather than functions, making software easier to design, maintain, and extend.
A Programming Paradigm is a style or approach used to write programs.
Some common programming paradigms are:
- Procedural Programming
- Object-Oriented Programming
- Functional Programming
- Event-Driven Programming
In this chapter, we focus on Object-Oriented Programming (OOP).
Procedural Programming is a programming paradigm in which a program is divided into a collection of functions.
Each function performs a specific task.
Example:
Student Management System
↓
Input Student Details
↓
Calculate Marks
↓
Display Result
The focus is on functions rather than objects.
Examples of procedural programming languages include:
- C
- Pascal
Python also supports procedural programming.
Although procedural programming is suitable for small programs, it has limitations when building large applications.
Some common limitations are:
- Code reusability is limited.
- Data is less secure because it can be accessed directly.
- Large programs become difficult to maintain.
- Debugging becomes more complex.
- Modifying one function may affect other parts of the program.
These limitations led to the development of Object-Oriented Programming.
Object-Oriented Programming helps solve many problems found in procedural programming.
It provides:
- Better code organization.
- Code reusability.
- Data security.
- Easier maintenance.
- Better scalability.
- Modular programming.
Because of these advantages, OOP is widely used in modern software development.
Object-Oriented Programming (OOP) is a programming paradigm in which software is designed using classes and objects.
A class acts as a blueprint, while objects represent real-world entities created from that blueprint.
An object contains:
- Data (Attributes)
- Behavior (Methods)
Consider a mobile phone.
Every mobile phone has:
Attributes:
- Brand
- Model
- Color
- Price
Behaviors:
- Call
- Message
- Camera
- Internet
Similarly, in Python:
Mobile
↓
Class
↓
Samsung Galaxy
↓
Object
The class defines the structure, while the object is an actual instance created from that structure.
Object-Oriented Programming provides the following features:
- Classes
- Objects
- Encapsulation
- Abstraction
- Inheritance
- Polymorphism
- Reusability
- Modularity
Object-Oriented Programming is built on four fundamental principles.
Encapsulation is the process of combining data and methods into a single unit called a class.
Example:
A Bank Account stores:
- Account Number
- Balance
and provides methods such as:
- Deposit()
- Withdraw()
The internal data is protected from direct access.
Abstraction means hiding unnecessary implementation details and showing only the essential features.
Example:
While driving a car, the driver uses:
- Steering Wheel
- Brake
- Accelerator
The driver does not need to know how the engine works internally.
Inheritance allows one class to acquire the properties and methods of another class.
Example:
Vehicle
↓
Car
↓
Electric Car
The child class automatically inherits features from the parent class.
Polymorphism means "many forms."
The same method can perform different actions depending on the object.
Example:
Method:
draw()
Objects:
- Circle
- Rectangle
- Triangle
Each object provides its own implementation of the draw() method.
Object-Oriented Programming offers several advantages:
- Code Reusability
- Easy Maintenance
- Better Security
- Modular Design
- Scalability
- Easy Testing
- Real-World Modeling
- Improved Readability
In this part, you learned:
- Programming Paradigms
- Procedural Programming
- Limitations of Procedural Programming
- Why OOP was introduced
- Object-Oriented Programming
- Real-World Analogy
- Features of OOP
- Four Pillars of OOP
A Class is a user-defined blueprint or template used to create objects.
A class defines:
- Attributes (Variables)
- Methods (Functions)
A class itself does not occupy significant memory for data. Memory is allocated only when an object is created.
class ClassName:
# Attributes
# MethodsExample
class Student:
passHere,
Studentis the class name.passis used because the class body is currently empty.
An Object is an instance of a class.
It is a real entity created from the blueprint (class).
Each object has its own memory and can access the variables and methods defined inside the class.
Blueprint
↓
House Plan
↓
Actual House
Similarly,
Class
↓
Student
↓
Student Object
object_name = ClassName()Example
student = Student()Here,
studentis an object.Student()creates the object.
Class
│
▼
Object Creation
│
▼
Object
(Occupies Memory)
Attributes are variables that belong to a class or an object.
They represent the properties of an object.
Example
Student
↓
Roll Number
Name
Age
Marks
These are attributes of the Student object.
Methods are functions defined inside a class.
They represent the behavior of an object.
Example
Student
↓
study()
writeExam()
displayResult()
self is a reference to the current object.
It allows an object to access its own attributes and methods.
Whenever an object calls a method, Python automatically passes the object as the first argument.
class Student:
def display(self):
print("Welcome")When executed,
student = Student()
student.display()Python internally converts it into
Student.display(student)Therefore,
self refers to the object student.
Consider
student = Student()Python performs the following steps internally.
Load Class
↓
Allocate Memory
↓
Create Object
↓
Return Object Reference
↓
Store Reference in Variable
student = Student()
student
│
▼
+---------------------+
| Student Object |
+---------------------+
The variable stores the reference to the object, not the object itself.
Write a Python program to create a class and an object.
class Student:
def display(self):
print("Welcome to Python OOP")
student = Student()
student.display()Welcome to Python OOP
Step 1
Student class created.
↓
Step 2
student object created.
↓
Step 3
display() method called.
↓
Step 4
Message displayed.
START
Create class
Create method
Create object
Call method
STOP
BEGIN
Create Student class
Create display() method
Create Student object
Call display()
END
class Student:Creates a class named Student.
def display(self):Creates a method inside the class.
print("Welcome to Python OOP")Displays the message.
student = Student()Creates an object of the Student class.
student.display()Calls the method using the object.
class Student:
def details(self):
print("Name : Rahul")
print("Age : 21")
student = Student()
student.details()Name : Rahul
Age : 21
| Class | Object |
|---|---|
| Blueprint | Instance |
| Logical entity | Physical entity |
| No separate data memory | Occupies memory |
| Used to create objects | Created from a class |
- Better code organization.
- Reusability.
- Easy maintenance.
- Modular programming.
- Real-world modeling.
- Easy debugging.
Classes and objects are used in:
- Banking Systems
- Hospital Management
- Student Management
- Railway Reservation
- Inventory Systems
- E-Commerce Applications
- Library Management Systems
- Use meaningful class names.
- Follow PascalCase for class names.
- Use meaningful method names.
- Keep one responsibility per class.
- Create reusable classes.
class Student:
def display(self):
print("Hello")No output will be produced until an object is created.
class Student:
def display():
print("Hello")This raises a TypeError because instance methods must include the self parameter.
display()This results in a NameError because the method belongs to the class and should be called using an object.
A programming paradigm that organizes programs using classes and objects.
A class is a blueprint used to create objects.
An object is an instance of a class.
It refers to the current object and allows access to its attributes and methods.
A class definition exists in memory, but data for instances is allocated when objects are created.
A class is a blueprint, while an object is an actual instance created from that blueprint.
- Create an Employee class and display employee details.
- Create a Car class and display car information.
- Create a Mobile class and print mobile specifications.
- Create two objects of the same class.
- Create a Book class with one method to display book details.
- OOP organizes programs using classes and objects.
- Class → Blueprint.
- Object → Instance of a class.
- Attributes store data.
- Methods define behavior.
selfrefers to the current object.- Objects occupy memory.
- OOP supports reusable and maintainable code.
In this chapter, you learned:
- Programming Paradigms
- Procedural Programming
- Need for OOP
- Object-Oriented Programming
- Features of OOP
- Four Pillars of OOP
- Class
- Object
- Attributes
- Methods
- self Keyword
- Object Creation
- Memory Representation
- Internal Working
- Practical Programs
- Best Practices
- Common Mistakes
- Interview Questions
- Practice Programs