Inheritance

Inheritance is a powerful concept in object-oriented programming. It's a way of organizing code, and it's particularly useful when you have a number of classes that are related to each other. Inheritance allows one class to inherit all the methods and properties from another class, promoting code reusability and logical relationships between classes.
To showcase the use of inheritance in Python, let's consider a zoo as an example. A zoo has various animals like dogs, cats, and birds. Although these animals have their unique behaviors, they also share some common characteristics. In such a scenario, inheritance can help us avoid code repetition and make the code more organized. Let's see how:
class Animal:                                         # Defining a base class `Animal`
    def __init__(self, name, age):                    # Constructor method with parameters `name` and `age`
        self.name = name                              # Assigning `name` attribute
        self.age = age                                # Assigning `age` attribute

    def eat(self):                                    # Common method `eat`
        return f'{self.name} is eating.'

    def make_sound(self):                             # Common method `make_sound`
        return f'{self.name} makes a sound.'

class Dog(Animal):                                    # Defining `Dog` class that inherits from `Animal`
    def make_sound(self):                             # Overriding the `make_sound` method
        return f'{self.name} barks.'

class Cat(Animal):                                    # Defining `Cat` class that inherits from `Animal`
    def make_sound(self):                             # Overriding the `make_sound` method
        return f'{self.name} meows.'

class Bird(Animal):                                   # Defining `Bird` class that inherits from `Animal`
    def eat(self):                                    # Overriding the `eat` method for `Bird`
        return f'{self.name} is pecking at its food.'

    def make_sound(self):                             # Overriding the `make_sound` method
        return f'{self.name} chirps.'

# The __init__ method is inherited from Animal
# but you can have __init__ for any of the inherited classes as well
dog = Dog('Rex', 5)                                   # Creating an instance of `Dog`
cat = Cat('Whiskers', 3)                              # Creating an instance of `Cat`
bird = Bird('Tweety', 2)                              # Creating an instance of `Bird`

for animal in [dog, cat, bird]:                       # Iterating over instances
    print(animal.eat())                               # Calling the inherited or overridden method `eat`
    print(animal.make_sound())                        # Calling the overridden method `make_sound`
    print('-----')
The output would be:
Rex is eating.
Rex barks.
-----
Whiskers is eating.
Whiskers meows.
-----
Tweety is pecking at its food.
Tweety chirps.
-----
In the above code, Animal is the parent class, while Dog, Cat, and Bird are child classes. The child classes inherit all methods from the Animal class. However, each child class overrides the make_sound method as the sound made by each animal is unique. We also override the eat method in the Bird class because the bird's eating style is different from the general eating behavior defined in the Animal class.
This approach allows us to represent real-world objects and their relationships more effectively, thereby creating a clear, concise, and maintainable codebase. So, inheritance enables us to enjoy the benefits of code reuse and reduction of complexity.

Challenge: Person & Employee Classes

You are asked to create two classes Person and Employee.
The Person class should contain the following attributes:
  • name: A string representing the name of the person.
  • age: An integer representing the age of the person.
The Person class should also contain the following method:
  • introduce_yourself(): This method prints a string in the following format: "My name is {name} and I am {age} years old."

The Employee class should inherit from the Person class and add the following attribute:
  • job_title: A string representing the job title of the person.
The Employee class should also override the following method:
  • introduce_yourself(): This method prints a string in the following format: "My name is {name}, I am {age} years old, and my job title is {job_title}."
Input
Output
alice=Person('Alice', 25); alice.introduce_yourself(); bob=Employee('Bob', 30, 'Engineer'); bob.introduce_yourself();
My name is Alice and I am 25 years old. My name is Bob, I am 30 years old, and my job title is Engineer.
 

Constraints

Time limit: 2 seconds

Memory limit: 512 MB

Output limit: 1 MB

To check your solution you need to sign in