Object-Oriented Programming (OOP) is one of the most powerful programming paradigms in Python. It helps you structure your code using objects and classes, making programs more organized, reusable, and scalable.
Python fully supports OOP and allows you to build real-world applications like games, websites, and software systems.
In this tutorial, you will learn all core Python OOP concepts with clear explanations and examples.
What is OOP?
OOP (Object-Oriented Programming) is a way of writing code using:
- Classes
- Objects
- Attributes
- Methods
It is based on real-world modeling.
Example:
- Class → Car design
- Object → Actual car
1. Class in Python
A class is a blueprint for creating objects.
Example:
class Car:
pass2. Object in Python
An object is an instance of a class.
Example:
class Car:
pass
car1 = Car()
print(car1)3. Constructor (init)
Constructor is automatically called when an object is created.
Example:
class Car:
def __init__(self, brand, model):
self.brand = brand
self.model = model
car1 = Car("Toyota", "Corolla")
print(car1.brand)
print(car1.model)4. Instance Variables
Variables that belong to an object.
Example:
class Student:
def __init__(self, name):
self.name = name
s1 = Student("John")
print(s1.name)5. Instance Methods
Methods that work with object data.
Example:
class Student:
def __init__(self, name):
self.name = name
def greet(self):
print("Hello", self.name)
s1 = Student("Alice")
s1.greet()6. Class Variables
Shared among all objects.
Example:
class Student:
school = "ABC School"
def __init__(self, name):
self.name = name
s1 = Student("John")
s2 = Student("Sara")
print(s1.school)
print(s2.school)7. Inheritance
One class inherits properties from another.
Example:
class Animal:
def speak(self):
print("Animal sound")
class Dog(Animal):
pass
d = Dog()
d.speak()8. Method Overriding
Child class changes parent method behavior.
Example:
class Animal:
def speak(self):
print("Animal sound")
class Dog(Animal):
def speak(self):
print("Bark")
d = Dog()
d.speak()9. Encapsulation
Hiding data using private variables.
Example:
class Bank:
def __init__(self):
self.__balance = 1000
def get_balance(self):
return self.__balance
b = Bank()
print(b.get_balance())10. Polymorphism
Same method name, different behavior.
Example:
class Cat:
def sound(self):
print("Meow")
class Dog:
def sound(self):
print("Bark")
animals = [Cat(), Dog()]
for animal in animals:
animal.sound()Real-World Example: Student System
class Student:
def __init__(self, name, marks):
self.name = name
self.marks = marks
def display(self):
print(self.name, self.marks)
s1 = Student("John", 85)
s2 = Student("Alice", 90)
s1.display()
s2.display()Real-World Example: Bank System
class BankAccount:
def __init__(self, owner, balance):
self.owner = owner
self.balance = balance
def deposit(self, amount):
self.balance += amount
def show_balance(self):
print(self.balance)
acc = BankAccount("John", 1000)
acc.deposit(500)
acc.show_balance()OOP Concepts Summary
| Concept | Description |
|---|---|
| Class | Blueprint of objects |
| Object | Instance of class |
| Constructor | Initializes object |
| Inheritance | Reuse code |
| Encapsulation | Hide data |
| Polymorphism | Multiple behaviors |
| Method Overriding | Redefine methods |
Common Mistakes
Mistake 1: Forgetting self
❌ Wrong:
def show():
print(name)✔ Must use self
Mistake 2: Not creating object
Car.show()✔ Wrong without object
Mistake 3: Confusing class and object
✔ Class is blueprint, object is real instance
Safe OOP Example
class Test:
def show(self):
print("Safe OOP method")
t = Test()
t.show()Conclusion
Python OOP is essential for building real-world applications with clean and reusable code.
You learned:
- Class and object basics
- Constructor and methods
- Inheritance and polymorphism
- Encapsulation concepts
- Real-world examples
Mastering OOP helps you become a strong Python developer and build scalable software systems.


0 Comments