Object-Oriented Programming (OOP) is a programming concept based on objects and classes.
It is used to organize code into reusable and structured components.
In simple words:
OOP allows you to model real-world things (like a car, student, bank account) into code.
KEY CONCEPTS OF OOP
There are four main principles of OOP:
- Encapsulation
- Abstraction
- Inheritance
- Polymorphism
1. CLASS
A class is a blueprint for creating objects.
Example:
class Student:
pass
2. OBJECT
An object is an instance of a class.
class Student:
passs1 = Student()
print(type(s1))
Here, s1 is an object of the Student class.
REAL-WORLD EXAMPLE
Think of a class as a blueprint of a house.
An object is the actual house built from that blueprint.
WHY USE OOP?
• Organizes large programs
• Makes code reusable
• Improves readability
• Helps manage complex projects
• Used in real-world applications
SIMPLE EXAMPLE WITH ATTRIBUTES
class Student:
def __init__(self, name, age):
self.name = name
self.age = ages1 = Student("Hira", 22)print(s1.name)
print(s1.age)
Here:
__init__is a constructorselfrefers to the current objectnameandageare attributes
EXAMPLE WITH METHODS
class Student:
def __init__(self, name):
self.name = name def greet(self):
print("Hello, my name is", self.name)s1 = Student("Ali")
s1.greet()
Methods are functions inside a class.
OOP VS PROCEDURAL PROGRAMMING
| Procedural | OOP |
|---|---|
| Uses functions | Uses classes & objects |
| Less structured for big projects | Highly structured |
| Harder to maintain large systems | Easier to manage large systems |
WHERE OOP IS USED
• Web development
• Game development
• Desktop applications
• Mobile apps
• Enterprise software
KEY TAKEAWAY
OOP is a programming approach that uses classes and objects to structure code in a clear, reusable, and real-world manner.
It is one of the most important concepts in Python and modern programming.