Interview Questions, Answers and Tutorials

Inheritance: “is-a” relationship

Inheritance: “is-a” relationship

Hey there! Today, we’re going to talk about a cool concept in programming called inheritance. Think of it like family traits, where kids inherit some features from their parents. In the world of programming, inheritance helps us to create new classes (like kids) that inherit properties and behaviors from existing classes (like parents).

What is Inheritance?

Inheritance allows us to define a new class based on an existing class. The new class (child) inherits attributes and methods (functions) from the existing class (parent). This helps us to reuse code and make our programs more organized.

The “is-a” Relationship

The “is-a” relationship means that one class is a specific type of another class. For example, if we have a class called Animal, and we created a class called Dog, we can say that a Dog is an Animal. This means Dog inherits the properties and behaviors of Animal.

Let’s See Some Code!

We’ll start with a simple example. First, we’ll create a class called Animal. Then, we’ll create a new class called Dog that inherits from Animal.

Creating the Animal Class

class Animal:
    def __init__(self, name):
        self.name = name

    def speak(self):
        print(f"{self.name} makes a sound")




Here, we have an Animal class with an __init__ method to set the name of the animal and a speak method to print a message.

Creating the Dog Class

class Dog(Animal):
    def speak(self):
        print(f"{self.name} barks")




In the Dog class, we inherit from the Animal class by putting (Animal) next to Dog. This means Dog gets all the attributes and methods of Animal. We also define a new speak method for Dog that overrides the one in Animal.

Using the Classes

my_pet = Dog("Buddy")
my_pet.speak()  # Output: Buddy barks




Here, we create a Dog object named “Buddy” and call its speak method. Since Dog inherits from Animal, it uses the __init__ method from Animal to set the name and its own speak method to print “Buddy barks”.

Practice Questions

  1. Create a Cat class that inherits from Animal. The Cat class should have its own speak method that prints “<name> meows”.

class Cat(Animal):
    def speak(self):
        print(f"{self.name} meows")




  1. Create an Elephant class that inherits from Animal. The Elephant class should have its own speak method that prints “<name> trumpets”.

class Elephant(Animal):
    def speak(self):
        print(f"{self.name} trumpets")




Solutions

Let’s check if our Cat and Elephant classes work as expected.

Testing the Cat Class

my_cat = Cat("Whiskers")
my_cat.speak()  # Output: Whiskers meows




Testing the Elephant Class

my_elephant = Elephant("Dumbo")
my_elephant.speak()  # Output: Dumbo trumpets




Great job! You’ve learned about inheritance and the “is-a” relationship. By using inheritance, we can create classes that share common features with parent classes, making our code more efficient and easier to manage. Keep practicing, and you’ll become a coding superstar in no time!