OOPs - Object-Oriented Programming
Goal of OOPs
OOPs is a way of structuring code so that:
It becomes more organized,
Easier to maintain and scale, and
Mirrors real-world entities (like customers, products, users, transactions).
Instead of writing one long script, we group data and functions together into logical βobjectsβ.
Core Concepts of OOPs
1. Class ποΈ
A class is like a blueprint or template.
It defines what an object will have (data) and can do (actions).
class Car:
def init(self, brand, model):
self.brand = brand # attribute
self.model = model # attribute
def drive(self): # method
print(f"{self.brand} {self.model} is driving.")
*2. Object (Instance) *
An object is a real-world entity created from a class.
You can create multiple objects from one class, each with its own data.
car1 = Car("Tesla", "Model S")
car2 = Car("BMW", "M5")
car1.drive() # Tesla Model S is driving.
car2.drive() # BMW M5 is driving.
3. Attributes & Methods
Attributes = Variables inside a class that store data about the object.
Methods = Functions inside a class that define behavior.
Think of a human object β attributes are name, age, gender and methods are walk(), talk(), sleep().
4. Encapsulation π
It means bundling data + methods together, so no one outside the class directly messes with them.
You can also make some data private (hidden) using _ or __.
class BankAccount:
def init(self, balance):
self.__balance = balance # private variable
def deposit(self, amount):
self.__balance += amount
print(f"New balance: {self.__balance}")
5. Inheritance π¨βπ§
It allows one class to inherit properties and behavior from another.
class Vehicle:
def move(self):
print("Moving...")
class Car(Vehicle):
def wheels(self):
print("Has 4 wheels")
c = Car()
c.move() # inherited method
c.wheels()
β Helps reuse code and build hierarchy β like Employee β Manager β CEO.
6. Polymorphism π§¬
Same method name, but different behaviors in different classes.
class Dog:
def speak(self):
print("Woof!")
class Cat:
def speak(self):
print("Meow!")
for animal in [Dog(), Cat()]:
animal.speak()
7. Abstraction π
Hiding complex details and showing only whatβs necessary.
For example, when you drive a car β you press the pedal (simple), but you donβt see the engine mechanics.
In code, you hide complexity behind functions or classes.
βοΈ Why OOP Matters
β
Keeps large projects manageable
β
Makes code reusable
β
Easier debugging
β
Scales naturally with new features
β
Mirrors real-world systems (useful in business software, games, AI models, etc.)
Top comments (1)
Follow for more......................