Python is a versatile language that supports both procedural and object-oriented programming (OOP). OOP is a powerful paradigm that helps organize code, improve reusability, and manage complexity by modeling real-world entities as objects.
In this guide, you’ll learn the fundamentals of OOP in Python, including classes, objects, methods, inheritance, encapsulation, and polymorphism — all with clear explanations and code examples.
What Is Object-Oriented Programming?
OOP is a way of structuring code by bundling related data and behaviors into reusable blueprints called classes, and actual instances of those blueprints are called objects.
Benefits of OOP:
- Organizes code into logical components
- Makes large codebases easier to maintain
- Encourages reusability through inheritance
- Helps avoid code duplication
Key OOP Concepts in Python
1. Class and Object
A class is a blueprint, while an object is a real-world instance of that blueprint.
class Dog:
def bark(self):
print("Woof!")
my_dog = Dog() # Creating an object
my_dog.bark() # Calling a method → Output: Woof!
2. Constructor (__init__
Method)
__init__()
is called automatically when you create a new object. It initializes object attributes.
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
def bark(self):
print(f"{self.name} says woof!")
dog1 = Dog("Buddy", 3)
dog1.bark() # Buddy says woof!
3. Attributes and Methods
- Attributes are variables tied to an object (
self.name
). - Methods are functions tied to an object.
print(dog1.name) # Buddy
dog1.bark() # Buddy says woof!
4. Encapsulation
Encapsulation is the bundling of data and methods into one unit — the class — and controlling access to it.
Private Attributes (convention with _
or __
):
class Account:
def __init__(self, balance):
self.__balance = balance # private variable
def deposit(self, amount):
self.__balance += amount
def get_balance(self):
return self.__balance
acc = Account(1000)
acc.deposit(500)
print(acc.get_balance()) # 1500
You can’t access __balance
directly from outside.
5. Inheritance
Inheritance lets one class (child) reuse the attributes and methods of another class (parent).
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def bark(self):
print("Dog barks")
d = Dog()
d.speak() # Inherited from Animal
d.bark()
6. Method Overriding
You can override methods in the child class to change behavior.
class Animal:
def speak(self):
print("Animal speaks")
class Cat(Animal):
def speak(self):
print("Meow")
c = Cat()
c.speak() # Meow
7. Polymorphism
Polymorphism allows different classes to use the same method name with different implementations.
class Bird:
def sound(self):
print("Chirp")
class Duck:
def sound(self):
print("Quack")
def make_sound(animal):
animal.sound()
b = Bird()
d = Duck()
make_sound(b) # Chirp
make_sound(d) # Quack
Summary of OOP Terminology
Concept | Description |
---|---|
Class | Blueprint for creating objects |
Object | Instance of a class |
Method | Function inside a class |
Attribute | Variable inside a class |
Encapsulation | Restricting direct access to data |
Inheritance | Creating a class based on another |
Polymorphism | Using common method names with different behavior |
Real-World Example: A Simple Banking App
class BankAccount:
def __init__(self, owner, balance=0):
self.owner = owner
self.__balance = balance
def deposit(self, amount):
self.__balance += amount
def withdraw(self, amount):
if amount > self.__balance:
print("Insufficient funds")
else:
self.__balance -= amount
def get_balance(self):
return self.__balance
account = BankAccount("Alice", 1000)
account.deposit(500)
account.withdraw(200)
print(account.get_balance()) # 1300
Final Tips for Mastering OOP in Python
- Always use
self
as the first parameter in instance methods - Use
__init__()
to initialize attributes - Practice with real-world entities like
Car
,Student
,Book
- Apply concepts gradually — don’t force OOP where it’s not needed
- Explore advanced OOP: multiple inheritance,
super()
, and@property
Where to Go Next?
Once you’re comfortable with classes and objects:
- Build a small project like a to-do app or student management system
- Explore design patterns (e.g., Singleton, Factory)
- Learn OOP with GUI using
tkinter
orPyQt
- Apply OOP in web frameworks like Flask and Django