Python > Object-Oriented Programming (OOP) in Python > Inheritance > Adding New Methods in Subclasses
Extending Functionality with New Methods in Subclasses
This code snippet demonstrates how to add new, unique methods to subclasses in Python, building upon the functionality inherited from the parent class. This is a key aspect of inheritance and allows for specialized behavior in derived classes.
Base Class: Animal
We define a base class `Animal` with an initializer and a `speak` method. This provides the foundation for our derived classes.
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return "Generic animal sound"
Subclass: Dog with a New Method
Here, we create a subclass `Dog` that inherits from `Animal`. We override the `speak` method to provide a dog-specific sound and, critically, we add a new method, `fetch`, which is unique to the `Dog` class. This exemplifies adding new functionality to a subclass.
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name)
self.breed = breed
def speak(self):
return "Woof!"
def fetch(self, item):
return f"{self.name} fetches the {item}!"
Subclass: Cat with a New Method
Similar to the `Dog` class, `Cat` inherits from `Animal`. We override the `speak` method to return "Meow!", and introduce a new method called `climb`. The `climb` method is unique to the `Cat` class, demonstrating specialized behavior through added methods.
class Cat(Animal):
def __init__(self, name, color):
super().__init__(name)
self.color = color
def speak(self):
return "Meow!"
def climb(self, tree):
return f"{self.name} climbs the {tree}!"
Usage Example
This section shows how to create instances of the `Dog` and `Cat` classes and call both the inherited and newly defined methods. Note that you can call `.speak()` on both instances but `.fetch()` is specific to dogs and `.climb()` is specific to cats.
my_dog = Dog("Buddy", "Golden Retriever")
my_cat = Cat("Whiskers", "Gray")
print(my_dog.speak())
print(my_dog.fetch("ball"))
print(my_cat.speak())
print(my_cat.climb("oak tree"))
Concepts Behind the Snippet
This snippet illustrates the core OOP concept of inheritance. Inheritance allows subclasses to inherit properties and methods from parent classes, promoting code reuse and a hierarchical structure. By adding new methods in subclasses, we extend the functionality of the parent class to suit the specific needs of the subclass. This enhances code modularity and maintainability.
Real-Life Use Case
Consider a software system for managing different types of vehicles. A base class `Vehicle` might have attributes like `speed` and `color`, and methods like `accelerate` and `brake`. Subclasses like `Car`, `Truck`, and `Motorcycle` could inherit these attributes and methods but also have their own specific methods. For example, `Car` could have a `use_navigation_system` method, `Truck` could have a `load_cargo` method, and `Motorcycle` could have a `lean_into_turn` method. Each subclass builds upon the base `Vehicle` class with its own unique functionality.
Best Practices
Interview Tip
Be prepared to discuss the benefits and drawbacks of inheritance. Highlight code reuse, modularity, and extensibility as advantages. Mention potential issues like increased complexity and the fragile base class problem as disadvantages. Also, know the difference between inheritance and composition. Explain the 'is-a' vs 'has-a' relationship.
When to Use Them
Use inheritance when you want to create a specialized version of an existing class, inheriting its common properties and behaviors while adding or modifying specific features. It's particularly useful when dealing with hierarchical relationships between objects.
Memory Footprint
Inheritance itself doesn't drastically increase the memory footprint. Each object of the subclass will store its own attributes and any overridden methods. However, deep inheritance hierarchies can indirectly increase memory usage due to the increased complexity and the potential for unnecessary attributes or methods being inherited.
Alternatives
Alternatives to inheritance include:
Pros
Cons
FAQ
-
Can a subclass inherit from multiple parent classes?
Yes, Python supports multiple inheritance. A class can inherit from multiple parent classes, inheriting their attributes and methods. However, multiple inheritance can introduce complexity and potential conflicts, so it should be used with caution. -
What is the purpose of the `super()` function?
The `super()` function is used to call methods from the parent class. It's commonly used in the subclass's `__init__` method to initialize the inherited attributes. It's also useful for calling overridden methods from the parent class when you want to extend their functionality. -
What happens if a subclass defines a method with the same name as a method in the parent class?
The subclass's method overrides the parent class's method. When the method is called on an instance of the subclass, the subclass's version of the method will be executed.