Understanding Inheritance in Python
Introduction:
Inheritance is a fundamental concept in object-oriented programming that allows a class to inherit attributes and methods from another class. This concept simplifies code reusability and enhances the organization of your code. In this blog post, we’ll explore inheritance in Python with practical examples to illustrate how it works.
What is Inheritance?
In inheritance, the original class is called the parent class (or base class), and the class that inherits from it is called the child class (or derived class). The child class automatically inherits all the attributes and methods of the parent class, allowing it to reuse existing code.
Creating a Child Class
Let’s start with a simple example. Suppose we have a Dog
class, and we want to create a Chihuahua
class that inherits from Dog
.
class Dog:
def __init__(self, name):
self.name = name
def speak(self):
print(f"{self.name} says bark")
# Child class
class Chihuahua(Dog):
pass
# Creating an instance of Chihuahua
chihuahua = Chihuahua("Roxy")
chihuahua.speak()
Output:
Roxy says bark
In this example, the Chihuahua
class inherits from the Dog
class. Even though the Chihuahua
class is empty, it has all the attributes and methods of the Dog
class.
Overriding Methods
Sometimes, the child class needs to modify the behavior of the parent class. This can be done by overriding methods in the child class.
class Chihuahua(Dog):
def speak(self):
print(f"{self.name} says yap, yap, yap")
# Creating an instance of Chihuahua
chihuahua = Chihuahua("Roxy")
chihuahua.speak()
Output:
Roxy says yap, yap, yap
Adding New Methods
The child class can also have additional methods that are not present in the parent class.
class Chihuahua(Dog):
def speak(self):
print(f"{self.name} says yap, yap, yap")
def wag_tail(self):
print(f"{self.name} is wagging its tail vigorously")
# Creating an instance of Chihuahua
chihuahua = Chihuahua("Roxy")
chihuahua.speak()
chihuahua.wag_tail()
Output:
Roxy says yap, yap, yap
Roxy is wagging its tail vigorously
Using the super Function
The super
function is used to call methods from the parent class. This is particularly useful when you want to extend the behavior of the parent class methods in the child class.
class Dog:
def __init__(self, name):
self.name = name
def speak(self):
print(f"{self.name} says bark")
class Chihuahua(Dog):
def __init__(self, name, size):
super().__init__(name)
self.size = size
def speak(self):
super().speak()
print(f"{self.name} says yap, yap, yap")
# Creating an instance of Chihuahua
chihuahua = Chihuahua("Roxy", "small")
chihuahua.speak()
print(f"Size: {chihuahua.size}")
Output:
Roxy says bark
Roxy says yap, yap, yap
Size: small
In this example, the __init__
and speak
methods in the Chihuahua
class use super()
to call the corresponding methods in the Dog
class. This ensures that the initialization and behavior of the parent class are preserved while adding new functionality.
Extending Built-in Classes
You can also extend Python’s built-in classes. For example, let’s create a custom list class that only allows unique items.
class UniqueList(list):
def append(self, item):
if item not in self:
super().append(item)
# Using the UniqueList class
unique_list = UniqueList()
unique_list.append(1)
unique_list.append(1)
unique_list.append(2)
print(unique_list)
Output:
[1,2]
Here, the UniqueList
class extends the built-in list
class and overrides the append
method to ensure that only unique items are added to the list.
Summary
Inheritance is a powerful feature in Python that allows for code reuse and a clean organization of your code. By understanding and utilizing inheritance, you can create more modular and maintainable programs. Experiment with these concepts in your projects to see how inheritance can simplify your code and enhance its functionality.