OOP Concepts in Python
Subject: Data Structures and Algorithms · Language: Python · Level: beginner · 7 min read
Learn encapsulation, inheritance, polymorphism, and abstraction in Python with simple student-friendly examples.
OOP stands for Object-Oriented Programming. It is a programming style where code is organized using classes and objects. Instead of writing everything as separate variables and functions, related data and behavior are grouped together.
Encapsulation means keeping data and methods together in a class. It also means controlling how data should be accessed or changed. Python uses naming conventions like `_value` to show that something should be treated as internal.
Inheritance allows one class to reuse features of another class. The parent class contains common behavior, and the child class can add or change behavior. This reduces repeated code when objects have an is-a relationship.
Polymorphism means the same method name can behave differently for different objects. For example, different classes can have a method named `speak()`, but each class can implement it in its own way.
Abstraction means showing only important details and hiding internal complexity. A user of a method should know what the method does, but does not always need to know exactly how it works inside.
In DSA, OOP appears in linked lists, trees, graphs, heaps, and custom structures. A node class is one of the simplest and most useful examples because it combines data with references.
Python supports OOP to make programs more organized, reusable, and easier to maintain. When a project becomes large, OOP helps divide the program into meaningful parts such as `Student`, `Account`, `Employee`, `Vehicle`, or `Node`.
The four main OOP concepts are encapsulation, inheritance, polymorphism, and abstraction. These are not just interview words; they explain how code is designed in real applications.
Encapsulation
Encapsulation means keeping data and related methods together inside a class.
It also helps protect data from direct unwanted access. In Python, strict private variables are not enforced like in some languages, but programmers use naming conventions to show that a value should be treated as internal.
```python
class Student:
def __init__(self):
self._marks = 0
def set_marks(self, marks):
if marks >= 0:
self._marks = marks
```
Here, marks are updated through a method so validation can be applied before storing the value.
Inheritance
Inheritance allows one class to reuse properties and methods of another class.
The class that gives features is called the parent class. The class that receives features is called the child class.
Inheritance is useful when two classes have an is-a relationship. For example, a `Dog` is an `Animal`, a `Car` is a `Vehicle`, and a `SavingsAccount` is an `Account`.
In Python, inheritance is written by placing the parent class name inside parentheses.
```python
class Animal:
pass
class Dog(Animal):
pass
```
This means `Dog` inherits from `Animal`.
Polymorphism
Polymorphism means one name can have different forms. In Python, different classes can have methods with the same name, and each class can provide its own behavior.
```python
class Dog:
def sound(self):
print("Bark")
class Cat:
def sound(self):
print("Meow")
```
Both classes have a method named `sound`, but the output is different.
Polymorphism makes code flexible because the same method name can work with different object types.
Abstraction
Abstraction means showing only important details and hiding internal complexity.
For example, when you use a phone call button, you do not need to know the internal network process. You only need to know what action to perform.
In Python, abstraction can be achieved using classes, methods, and abstract base classes from the `abc` module. Beginners should first understand the idea before going deep into abstract classes.
Why OOP Matters
OOP is important because it helps programmers write code that is easier to expand. If tomorrow a project needs a new type of user, product, account, or vehicle, classes and objects make the extension cleaner.
Example Program
```python
class Animal:
def eat(self):
print("Dog is eating")
class Dog(Animal):
def bark(self):
print("Dog is barking")
dog = Dog()
dog.eat()
dog.bark()
```
Output
```text
Dog is eating
Dog is barking
```
In this program, `Animal` is the parent class and `Dog` is the child class. The `Dog` object can call `bark()` from its own class and `eat()` from the parent class. This shows simple inheritance.
OOP in DSA
OOP is often used when a data structure needs a custom shape. A linked list node stores data and a next reference. A tree node stores data, left child, and right child. A graph edge may store source, destination, and weight.
Classes help group this related information into one meaningful unit.
Object-Oriented Programming is a way to organize code around objects. An object combines data and behavior. This helps programmers model real-world entities such as students, accounts, products, vehicles, nodes, and users.
Inheritance allows one class to reuse features of another class. If `Dog` extends `Animal`, then `Dog` can reuse common animal behavior and also add its own behavior. This avoids repeating the same code in many related classes.
Practice Question
Create a parent class `Vehicle` and a child class `Car`. Add one method in each class, create a `Car` object, and call both methods.