JitCoder

OOP in Python Explained with Examples (2026)

Object-Oriented Programming (OOP) is one of the most important programming paradigms used in modern software development. Python fully supports Object-Oriented Programming, making it easier to create reusable, scalable, and maintainable applications.

If you’re learning Python, understanding OOP in Python is essential because it is widely used in web development, machine learning, game development, desktop applications, and enterprise software.

In this guide, you’ll learn the core concepts of OOP in Python with practical examples.

What is OOP in Python?

Object-Oriented Programming (OOP) is a programming approach that organizes code into objects and classes rather than functions and logic alone.

A class acts as a blueprint, while an object is an actual instance created from that blueprint.

For example:

  • Class = Car Blueprint
  • Object = Specific Car

Python allows developers to model real-world entities using classes and objects.

Real-Life Example

Imagine a student management system.

Each student has:

  • Name
  • Roll Number
  • Marks

Instead of creating separate variables repeatedly, we can create a Student class and generate multiple student objects.


Why Use OOP?

OOP helps developers:

  • Reuse code efficiently
  • Improve readability
  • Reduce code duplication
  • Simplify maintenance
  • Enhance security
  • Build scalable applications

Large applications become easier to manage when built using OOP principles.

python docs


Understanding Class and Object

What is a Class?

A class is a blueprint used to create objects.

Syntax

class Student:    pass

What is an Object?

An object is an instance of a class.

class Student:    passstudent1 = Student()student2 = Student()

Here:

  • Student is a class
  • student1 and student2 are objects

Creating a Class with Attributes

class Student:    name = "Abinash"    course = "Python"student = Student()print(student.name)print(student.course)

Output

AbinashPython

Attributes store data related to an object.


Constructor in Python

A constructor initializes object data automatically when an object is created.

Python uses the __init__() method as a constructor.

Example

class Student:    def __init__(self, name, course):        self.name = name        self.course = coursestudent1 = Student("Rahul", "Python")print(student1.name)print(student1.course)

Output

RahulPython

Understanding self Keyword

The self keyword refers to the current object.

Example

class Employee:    def __init__(self, name):        self.name = nameemp = Employee("John")print(emp.name)

Without self, Python cannot identify which object’s data is being accessed.


Instance Variables and Methods

Instance Variables

Variables that belong to an object.

Example

class Car:    def __init__(self, brand):        self.brand = brand

Instance Methods

Functions defined inside a class.

class Car:    def __init__(self, brand):        self.brand = brand    def display(self):        print("Brand:", self.brand)car = Car("Toyota")car.display()

Output

Brand: Toyota

Encapsulation in Python

Encapsulation means hiding internal implementation details and restricting direct access to data.

Example

class BankAccount:    def __init__(self):        self.__balance = 1000    def show_balance(self):        return self.__balanceaccount = BankAccount()print(account.show_balance())

Output

1000

Trying to access:

print(account.__balance)

Produces an error because the variable is private.

Benefits of Encapsulation

OOP in Python

  • Better security
  • Controlled access
  • Improved maintainability

Inheritance in Python

Inheritance allows one class to inherit properties and methods from another class .

Parent Class

class Animal:    
def sound(self):        
print("Animal makes sound")
class Employee:

    def __init__(self, name, salary):
        self.name = name
        self.salary = salary

    def display(self):
        print("Name:", self.name)
        print("Salary:", self.salary)

emp1 = Employee("Rahul", 50000)
emp2 = Employee("Amit", 60000)

emp1.display()
print()

emp2.display()

Child Class

class Dog(Animal):    passdog = Dog()dog.sound()

Output

Animal makes sound

The Dog class automatically inherits methods from Animal.


Types of Inheritance in Python

Single Inheritance

class A:    passclass B(A):    pass

Multiple Inheritance

class A:    passclass B:    passclass C(A, B):    pass

Multilevel Inheritance

class A:    passclass B(A):    passclass C(B):    pass

Hierarchical Inheritance

class Parent:    passclass Child1(Parent):    passclass Child2(Parent):    pass

Method Overriding

Method overriding occurs when a child class provides its own implementation of a parent method.

Example

class Animal:    def sound(self):        print("Animal Sound")class Dog(Animal):    def sound(self):        print("Bark")dog = Dog()dog.sound()

Output

Bark

The child class overrides the parent method.


Polymorphism in Python

Polymorphism means “many forms.”

The same method name behaves differently depending on the object.

Example

class Dog:    def sound(self):        print("Bark")class Cat:    def sound(self):        print("Meow")animals = [Dog(), Cat()]for animal in animals:    animal.sound()

Output

BarkMeow

The same method sound() behaves differently.

Benefits of Polymorphism

  • Flexible code
  • Better scalability
  • Easier maintenance

Abstraction in Python

Abstraction hides implementation details and shows only essential functionality.

Python provides abstraction using the abc module.

Example

from abc import ABC, abstractmethodclass Vehicle(ABC):    @abstractmethod    def start(self):        passclass Car(Vehicle):    def start(self):        print("Car Started")car = Car()car.start()

Output

Car Started

Users only know the method exists, not how it works internally.


Access Modifiers in Python

Python supports three access levels.

Public

Accessible from anywhere.

class Demo:    name = "Python"

Protected

Uses a single underscore.

class Demo:    _name = "Python"

Private

Uses double underscore.

class Demo:    __name = "Python"

Private variables cannot be directly accessed outside the class.


Real-World OOP Example

Let’s create a simple employee management system.

class Employee:    def __init__(self, name, salary):        self.name = name        self.salary = salary    def display(self):        print("Name:", self.name)        print("Salary:", self.salary)emp1 = Employee("Rahul", 50000)emp2 = Employee("Amit", 60000)emp1.display()print()emp2.display()

Output

Name: RahulSalary: 50000Name: AmitSalary: 60000

This example demonstrates:

  • Class creation
  • Object creation
  • Constructor usage
  • Instance methods

OOP vs Procedural Programming

FeatureOOPProcedural
StructureClasses and ObjectsFunctions
ReusabilityHighLimited
SecurityBetterLower
MaintenanceEasyDifficult
ScalabilityHighMedium
Real-world ModelingExcellentLimited

For large projects, OOP is generally preferred.


Advantages of OOP in Python

1. Code Reusability

Inheritance helps reuse existing code.

2. Modularity

Code is divided into independent classes.

3. Security

Encapsulation protects sensitive data.

4. Easy Maintenance

Changes can be made without affecting the entire application.

5. Scalability

Large systems can be expanded easily.

6. Better Testing

Individual classes can be tested independently.


Common Mistakes Beginners Make

Forgetting self

Incorrect:

def display():    pass

Correct:

def display(self):    pass

Not Using Constructors

Constructors simplify object initialization.

Excessive Inheritance

Avoid creating deep inheritance chains unless necessary.

Ignoring Encapsulation

Private variables improve data protection.


Best Practices for OOP in Python

Use Meaningful Class Names

Good:

class Student:

Bad:

class S:

Keep Classes Focused

Each class should handle a single responsibility.

Prefer Composition When Appropriate

Avoid unnecessary inheritance.

Use Encapsulation

Protect sensitive data.

Write Reusable Methods

Methods should perform specific tasks.


Applications of OOP in Python

OOP is widely used in:

  • Web Development
  • Machine Learning
  • Artificial Intelligence
  • Game Development
  • Desktop Applications
  • Banking Systems
  • E-commerce Platforms
  • Inventory Management Systems
  • ERP Software
  • Data Science Projects

Frameworks like Django heavily rely on OOP concepts.


Conclusion

Understanding OOP in Python is a crucial step for every Python developer. Object-Oriented Programming helps create clean, reusable, and maintainable code by organizing programs into classes and objects.

The four pillars of OOP:

  1. Encapsulation
  2. Inheritance
  3. Polymorphism
  4. Abstraction

form the foundation of modern software development. Once you master these concepts, you’ll be able to build professional Python applications more efficiently.

Whether you’re interested in web development, machine learning, automation, or enterprise software, learning OOP in Python will significantly improve your coding skills and prepare you for real-world programming projects.

Similar Post,

Python Functions Explained

Python Exception Handling

Python File Handling

Python Recursion Explained

NumPy Basics Explained

Leave a Comment

Your email address will not be published. Required fields are marked *