Python as an Object-Oriented Programming (OOP) Language
Object-Oriented Programming (OOP) Language
Python is not just a powerful scripting language—it is also a fully object-oriented programming (OOP) language. OOP is a programming paradigm that organizes code into objects, which bundle data (attributes) and functions (methods) together.
Object-Oriented Programming helps in writing clean, reusable, and modular code, which is easier to maintain and extend.
![]() |
| Object Oriented Programming |
OOP Concepts in Python
Python supports all four major principles of Object-Oriented Programming:
-
Encapsulation
-
Abstraction
-
Inheritance
-
Polymorphism
Let’s explore each concept with examples.
1. Encapsulation
Encapsulation means wrapping data and methods into a single unit.
It restricts direct access to some components, protecting the object’s internal state.
Example:
class Student:
def __init__(self, name, age):
self.name = name # public variable
self.__age = age # private variable
def display(self):
print("Name:", self.name)
print("Age:", self.__age)
s = Student("Alice", 21)
s.display()
# Accessing private variable directly will raise an error:
# print(s.__age) ❌
Note- Here, __age is private and cannot be accessed directly outside the class.
2. Abstraction
Abstraction means hiding complex details and showing only the necessary parts.
In Python, abstraction can be achieved using abstract classes (from the abc module).
Example:
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self):
pass
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14 * self.radius * self.radius
c = Circle(5)
print("Area of circle:", c.area())
3. Inheritance
Inheritance allows one class to derive attributes and methods from another class.
It helps in code reusability and creating hierarchies.
Example:
class Animal:
def speak(self):
print("Animals make sound")
class Dog(Animal):
def speak(self):
print("Dog barks")
a = Animal()
a.speak()
d = Dog()
d.speak()
4. Polymorphism
Polymorphism means same function name but different behaviors depending on the object type.
Example:
class Bird:
def intro(self):
print("There are many types of birds")
def flight(self):
print("Most birds can fly")
class Penguin(Bird):
def flight(self):
print("Penguins cannot fly but swim")
b1 = Bird()
b2 = Penguin()
for bird in (b1, b2):
bird.intro()
bird.flight()
The flight() method behaves differently for Bird and Penguin objects.
Advantages of OOP in Python
- Reusability – Code can be reused through inheritance.
- Scalability – Easier to expand large programs.
- Maintainability – Code changes are isolated in specific classes.
- Data Security – Encapsulation hides internal data.
- Modularity – Each class is a self-contained module.
Summary
| Concept | Description | Example Keyword |
|---|---|---|
| Encapsulation | Binding data & methods | __init__, private variables |
| Abstraction | Hiding details, showing essentials | ABC, @abstractmethod |
| Inheritance | Acquiring properties of another class | class SubClass(ParentClass) |
| Polymorphism | Many forms, same interface | Method overriding |
Conclusion
Python’s simplicity and flexibility make it an excellent language for implementing Object-Oriented Programming concepts. By using classes, objects, inheritance, and polymorphism, developers can build scalable, maintainable, and efficient programs.

Comments
Post a Comment