Interview Questions, Answers and Tutorials



Hey, kiddo!

Today, we’re going to dive into something super cool called “Inheritance” in Python. Imagine you have a toy box full of different toys. Some are cars, some are dolls, and some are action figures. Now, each of these toys has something special about them, right? Like cars have wheels, dolls can talk, and action figures can move their arms and legs.

In Python, we have something similar called classes. Classes are like blueprints for creating objects, just like how the blueprints of a toy tell the factory workers how to make it. And guess what? With inheritance, we can make new classes that are like the old ones but with some extra features or changes, just like how you might add new features to your toys!

Let’s see some Python code to understand it better:

# Parent class
class Toy:
    def __init__(self, name): = name

    def play(self):
        print(f"Playing with {}")

# Child class inheriting from Toy
class Car(Toy):
    def __init__(self, name, color):
        self.color = color

    def drive(self):
        print(f"{} is driving in {self.color} color")

# Child class inheriting from Toy
class Doll(Toy):
    def talk(self):
        print(f"{} says: Hi, I'm a doll!")

# Child class inheriting from Car
class RemoteControlCar(Car):
    def __init__(self, name, color, remote_type):
        super().__init__(name, color)
        self.remote_type = remote_type

    def control(self):
        print(f"Controlling {} with {self.remote_type} remote")

# Creating objects of different classes
my_car = Car("Speedy", "red")
my_doll = Doll("Anna")
my_rc_car = RemoteControlCar("Racer", "blue", "wireless")

# Playing with objects

In this code, we have a Toy class that is like the base class. Then, we have Car and Doll classes which are like specialized versions of Toy. The RemoteControlCar class is a special type of Car that can be controlled remotely.

Now, let’s have some practice questions to see if you’ve got the hang of it:

  1. What is inheritance in Python?
  2. Explain with an example how inheritance helps in code reusability.
  3. How can you call a method from the parent class inside a method of the child class?
  4. Create a new class called Superhero which inherits from Toy and has a method fly().

And here are the solutions:

  1. Inheritance in Python is a way to create a new class using an existing class. The new class can inherit the attributes and methods of the existing class, and it can also have its unique attributes and methods.
  2. Inheritance helps in code reusability by allowing us to create new classes that inherit the behavior of existing classes. Instead of writing the same code again, we can simply inherit from the existing class and add or modify the specific features we need in the new class.
  3. We can call a method from the parent class inside a method of the child class using the super() function. For example, super().parent_method() will call parent_method() of the parent class from within the method of the child class.
  4. Here’s the solution for creating the Superhero class:

# Child class inheriting from Toy
class Superhero(Toy):
    def fly(self):
        print(f"{} is flying like a superhero!")

Now you know all about inheritance in Python! Keep practicing and exploring, and soon you'll be a Python superhero yourself! 🚀🐍