Object-Oriented Programming (OOP) in Python
Course Title: Modern Python Programming: Best Practices and Trends
Section Title: Object-Oriented Programming (OOP) in Python
Topic: Inheritance, polymorphism, encapsulation, and abstraction in Python
Introduction
In the previous topic, we explored the basics of Object-Oriented Programming (OOP) in Python, including classes, objects, and methods. In this topic, we'll delve deeper into four fundamental concepts that form the backbone of OOP: inheritance, polymorphism, encapsulation, and abstraction. These concepts are essential for writing robust, maintainable, and efficient code.
Inheritance
Inheritance is a mechanism that allows one class to inherit the properties and behavior of another class. The child class (or subclass) inherits all the attributes and methods of the parent class (or superclass) and can also add new attributes and methods or override the ones inherited from the parent class.
Example:
class Vehicle:
def __init__(self, brand, model):
self.brand = brand
self.model = model
def start_engine(self):
print("Engine started")
class Car(Vehicle):
def __init__(self, brand, model, color):
super().__init__(brand, model)
self.color = color
def lock_doors(self):
print("Doors locked")
my_car = Car("Toyota", "Camry", "Blue")
my_car.start_engine() # Output: Engine started
my_car.lock_doors() # Output: Doors locked
In the above example, the Car
class inherits the start_engine
method from the Vehicle
class and adds a new method lock_doors
. The super()
function is used to call the constructor of the parent class.
Polymorphism
Polymorphism is the ability of an object to take on multiple forms. This can be achieved through method overriding or method overloading. Method overriding occurs when a child class provides a different implementation of a method that is already defined in the parent class. Method overloading occurs when multiple methods have the same name but different parameters.
Example:
class Shape:
def area(self):
pass
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14 * (self.radius ** 2)
class Rectangle(Shape):
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.width * self.height
circle = Circle(5)
rectangle = Rectangle(4, 5)
print(circle.area()) # Output: 78.5
print(rectangle.area()) # Output: 20
In the above example, the Circle
and Rectangle
classes override the area
method of the Shape
class. This is an example of method overriding.
Encapsulation
Encapsulation is the concept of bundling data and methods that manipulate that data into a single unit, called a class. This helps to hide the implementation details of the class from the outside world and provides a way to control access to the data.
Example:
class BankAccount:
def __init__(self, account_number, balance):
self.__account_number = account_number
self.__balance = balance
def deposit(self, amount):
self.__balance += amount
def get_balance(self):
return self.__balance
account = BankAccount("123456789", 1000)
account.deposit(500)
print(account.get_balance()) # Output: 1500
In the above example, the BankAccount
class encapsulates the account number and balance. The __
prefix before the attribute names is used to indicate that they are private and should not be accessed directly from outside the class.
Abstraction
Abstraction is the concept of showing only the necessary information to the outside world and hiding the implementation details. This helps to reduce complexity and improve modularity.
Example:
class CoffeeMachine:
def __init__(self):
self.__water_temp = 0
def make_coffee(self):
self.__heat_water()
self.__brew_coffee()
def __heat_water(self):
self.__water_temp = 200
def __brew_coffee(self):
print("Coffee is ready")
machine = CoffeeMachine()
machine.make_coffee() # Output: Coffee is ready
In the above example, the CoffeeMachine
class abstracts the details of making coffee. The make_coffee
method is the only public method, and the implementation details of heating the water and brewing the coffee are hidden.
Conclusion
Inheritance, polymorphism, encapsulation, and abstraction are fundamental concepts in Object-Oriented Programming. They help to promote code reuse, modularity, and maintainability. By understanding these concepts, you can write more efficient and effective code.
Further Reading
- Python documentation on Classes
- Real Python article on Inheritance
- geeksforgeeks article on Polymorphism in Python
- W3Schools article on Encapsulation in Python
- Tutorialspoint article on Abstraction in Python
Leave a Comment or Ask for Help
If you have any questions or need further clarification on any of the concepts covered in this topic, please leave a comment below.
Images

Comments