Intermediate Python

Extending Functionality with super()

In Python, super() is a built-in function used to call a method from a parent class. It's especially useful in class inheritance when you want to extend or modify the behavior of a method from the superclass (parent class) in the subclass (child class). super() is also crucial for the proper initialization of subclasses.
To understand how super() can be used with methods and class initialization, let's use the example of animals making sounds. We'll create an Animal class and two subclasses, Dog and Cat.
class Animal:                       # Define a class named 'Animal'
    def __init__(self, species):
        self.species = species

    def speak(self):
        return 'Sounds...'

class Dog(Animal):                  # Define a subclass named 'Dog' that inherits from 'Animal'
    def __init__(self, name):
        super().__init__('Dog')     # Call the parent's '__init__' method using 'super()'
        self.name = name            # Additional attribute 'name' for 'Dog'
        print(self.species)         # Will print Dog

    def speak(self):
        original = super().speak()  # Call the parent's 'speak' method using 'super()'
        return original + ' Woof!'

class Cat(Animal):                  # Define a subclass named 'Cat' that inherits from 'Animal'
    def __init__(self, name):
        super().__init__('Cat')     # Call the parent's '__init__' method using 'super()'
        self.name = name            # Additional attribute 'name' for 'Cat'

    def speak(self):
        original = super().speak()  # Call the parent's 'speak' method using 'super()'
        return original + ' Meow!'

dog = Dog('Rex')                              # Create an instance of the 'Dog' class
cat = Cat('Fluffy')                           # Create an instance of the 'Cat' class
print(dog.name, dog.species, dog.speak())     # Rex Dog Sounds... Woof!
print(cat.name, cat.species, cat.speak())     # Fluffy Cat Sounds... Meow!
Here, both Dog and Cat subclasses use super().__init__() to call the __init__() method from their superclass Animal. This ensures that each instance of Dog or Cat is properly initialized with the respective species name. Similarly, the speak() method in both subclasses uses super().speak() to call the speak() method from Animal, allowing them to include the original animal sound in their own speak() methods.
So, the super() function allows a subclass to leverage and extend the behavior of its superclass. It plays a crucial role in enhancing code reusability, ensuring proper initialization of subclasses, and keeping your code modular and readable. This ultimately makes it a powerful tool in object-oriented programming with Python.

Challenge: Vehicle Classes

You are tasked to create classes for different kinds of vehicles. These classes should simulate some properties and behavior of real-world vehicles.
  1. Vehicle class: The base class. It should have the following attributes:
      • make: represents the manufacturer of the vehicle.
      • model: represents the specific model of the vehicle.
      • year: represents the year the vehicle was manufactured.
      • A method named honk() that, when called, prints the string Honk!.
  1. Car class: This class should inherit from the Vehicle class. It should have all the attributes and methods of the Vehicle class, plus the following:
      • color: represents the color of the car.
      • It should override the honk() method of the Vehicle class. When called, this method should print the string Beep beep!.
  1. Bicycle class: This class should also inherit from the Vehicle class. It should have all the attributes and methods of the Vehicle class but it needs to override the honk() method. When called, this method should print Ring ring!.
Your task is to implement these classes and methods.
Input
Output
car=Car('Tesla', 'Model S', 2022, 'Red'); bicycle=Bicycle('Schwinn', 'Fastback', 2020); car.honk(); bicycle.honk()
Beep beep! Ring ring!
 

Constraints

Time limit: 0.2 seconds

Memory limit: 512 MB

Output limit: 1 MB

To check your solution you need to sign in
Sign in to continue