OOP: Inheritance and Polymorphism
Inheritance and polymorphism are two of the most powerful features of Object-Oriented Programming (OOP). In this module, we will cover these concepts in detail, including how inheritance allows classes to inherit behavior from other classes, how polymorphism lets us treat different objects in a uniform way, and how method overriding works in Python.
Subtopic 1: Understanding Inheritance
Inheritance is a mechanism in OOP that allows a class (known as the child or subclass) to inherit the properties and behaviors (methods) of another class (the parent or base class). This promotes code reusability, where the child class can reuse, modify, or extend the functionality of the parent class.
Example of Inheritance:
# Parent class
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return f"{self.name} makes a sound."
# Child class inheriting from Animal
class Dog(Animal):
def speak(self):
return f"{self.name} barks."
# Child class inheriting from Animal
class Cat(Animal):
def speak(self):
return f"{self.name} meows."
# Creating objects
dog = Dog("Buddy")
cat = Cat("Whiskers")
print(dog.speak()) # Output: Buddy barks.
print(cat.speak()) # Output: Whiskers meows.
In this example:
DogandCatare subclasses of theAnimalclass.- Both subclasses inherit the
__init__method fromAnimalbut override thespeakmethod.
Subtopic 2: Method Overriding
Method overriding occurs when a subclass provides a specific implementation for a method that is already defined in its parent class. The method in the subclass replaces the method in the parent class when called on an instance of the subclass.
Example of Method Overriding:
class Parent:
def show(self):
return "This is the parent class."
class Child(Parent):
def show(self):
return "This is the child class."
# Creating objects
parent = Parent()
child = Child()
print(parent.show()) # Output: This is the parent class.
print(child.show()) # Output: This is the child class.
In this example:
- The
Childclass overrides theshowmethod from theParentclass to provide its own behavior.
Subtopic 3: Polymorphism in Python
Polymorphism allows methods in different classes to have the same name, but behave differently based on the type of object calling the method. It enables you to use a unified interface for different data types or objects.
Example of Polymorphism:
class Bird:
def speak(self):
return "Tweet tweet!"
class Dog:
def speak(self):
return "Woof!"
# Creating objects
bird = Bird()
dog = Dog()
# Polymorphism: same method name, different behavior
print(bird.speak()) # Output: Tweet tweet!
print(dog.speak()) # Output: Woof!
In this example:
- Both
BirdandDoghave a method namedspeak(), but each class implements it differently. This is an example of polymorphism.
Polymorphism allows different objects (from different classes) to respond to the same method name with behavior specific to their class. It is commonly used in situations where you want to generalize operations across multiple object types.
Subtopic 4: Using super() Function
The super() function allows you to call methods from a parent class in a child class, especially useful when you override a method in a child class but still want to execute the original method in the parent class. It is commonly used in the constructor (__init__) to ensure the parent class is properly initialized.
Example of super() Function:
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return f"{self.name} makes a sound."
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name) # Calls the __init__ method of Animal class
self.breed = breed
def speak(self):
return f"{self.name} barks."
# Creating an object
dog = Dog("Buddy", "Golden Retriever")
print(dog.speak()) # Output: Buddy barks.
In this example:
- The
super().__init__(name)call in theDogclass constructor ensures that thenameattribute is initialized using the__init__method from theAnimalclass. - This allows the
Dogclass to extend the functionality of theAnimalclass while still leveraging its constructor.
Tasks
-
Task 1: Animal Class and Inheritance
- Create a base class
Animalwith methodseat()andsleep(). Create two subclassesBirdandFish, each implementing their own version of theeat()method. Use inheritance to demonstrate shared behavior and method overriding.
- Create a base class
-
Task 2: Vehicle Class with Method Overriding
- Create a
Vehicleclass with aninfo()method that returns "This is a vehicle". Then create two subclasses:CarandBike. Override theinfo()method in both subclasses to include specific information for each vehicle type.
- Create a
-
Task 3: Employee Inheritance
- Create a class
Employeewith attributesnameandsalary. Then create two subclassesManagerandDeveloperthat inherit fromEmployee. Each subclass should have a method to print the employee's name, salary, and role.
- Create a class
-
Task 4: Shape Class with Polymorphism
- Define a
Shapeclass with a methodarea(). Then create two subclasses,RectangleandCircle, each implementing its own version ofarea(). Create instances of both classes and callarea()to demonstrate polymorphism.
- Define a
-
Task 5: Bank Account with
super()- Create a class
BankAccountwith a methoddeposit()and abalanceattribute. Create a subclassSavingsAccountthat inherits fromBankAccountand adds awithdraw()method. Usesuper()to call thedeposit()method from the parent class.
- Create a class
-
Task 6: Method Overriding and Super
- Create a
Personclass with a methodgreet(). Create a subclassEmployeethat overrides thegreet()method. Usesuper()to call thegreet()method from thePersonclass and extend it with an additional message.
- Create a