In the world of programming, there are often times when we need to reuse code in different classes. This is where the concept of inheritance comes into play. Inheritance allows us to create a new class that inherits the properties and methods of an existing class, known as the base class. In Python, this is achieved through the use of the keyword "class" followed by the name of the new class and the name of the base class in parentheses. However, what if we need to add new functionalities or modify existing ones in the base class? This is where extending base classes in Python comes into the picture.
To extend a base class in Python, we use the concept of method overriding. Method overriding is the process of redefining a method of the base class in the child class. This means that the child class will have its own implementation of the method, which will be used instead of the one defined in the base class. Let's take a look at an example to understand this better.
Suppose we have a base class called "Animal" with a method called "speak" which prints the sound that the animal makes. We also have a child class called "Dog" which inherits from the "Animal" class. However, dogs bark instead of making generic animal sounds. So, we need to override the "speak" method in the "Dog" class to print "bark" instead of the generic sound.
```
class Animal:
def speak(self):
print("I am an animal")
class Dog(Animal):
def speak(self):
print("bark")
```
In the above code, we have redefined the "speak" method in the "Dog" class. Now, when we create an instance of the "Dog" class and call the "speak" method, it will print "bark" instead of "I am an animal".
```
dog = Dog()
dog.speak() # Output: bark
```
But what if we want to add new functionalities to the base class? This is where the concept of super() comes in. The super() method allows us to access the methods and properties of the base class from within the child class. This is particularly useful when we want to add new functionalities to the existing methods of the base class. Let's see an example of this.
Suppose we want to add a new method called "eat" in the "Animal" class which prints "I am eating". We also want to add a new method called "eat" in the "Dog" class which will print "I am eating bones" instead of the generic message. We can achieve this by using super() to call the "eat" method of the base class and then adding the extra functionality.
```
class Animal:
def speak(self):
print("I am an animal")
def eat(self):
print("I am eating")
class Dog(Animal):
def speak(self):
print("bark")
def eat(self):
super().eat()
print("bones")
```
In the above code, we have used super() to call the "eat" method of the base class and then added the extra functionality to print "bones". Now, when we call the "eat" method on an instance of the "Dog" class, it will print "I am eating" followed by "bones".
```
dog = Dog()
dog.eat() # Output: I am eating bones
```
In conclusion, extending base classes in Python allows us to modify and add new functionalities to existing classes without having to rewrite the entire code. This makes our code more efficient and helps in avoiding code duplication. So, the next time you come across a situation where you need to modify a base class in Python, remember the concepts of method overriding and super() and extend your base class with ease.