Python - Inheritance

Why should I learn to solve Python: Inheritance technical interview questions?

Learn and practise solving Python: Inheritance technical interview questions and answers to enhance your skills for clearing technical interviews, HR interviews, campus interviews, and placement tests.

Where can I get technical Python: Inheritance technical interview questions and answers with explanations?

IndiaBIX provides you with lots of fully solved Python: Inheritance technical interview questions and answers with a short answer description. You can download Python: Inheritance technical interview questions and answers as PDF files or e-books.

How do I answer Python: Inheritance technical interview questions from various companies?

You can answer all kinds of Python: Inheritance technical interview questions by practising the given exercises (short answer type). You can also find the frequently asked Python: Inheritance technical interview questions with answers from various companies, such as TCS, Wipro, Infosys, CTS, IBM, etc.

1.
What is inheritance in Python?

Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class (subclass/derived class) to inherit attributes and methods from another class (superclass/base class). It promotes code reuse and supports the concept of a hierarchy in programming. In Python, inheritance is implemented using the 'class' keyword and specifying the base class in parentheses.

Let's consider an example to illustrate inheritance in Python:

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

    def make_sound(self):
        pass

class Dog(Animal):
    def make_sound(self):
        return "Woof!"

class Cat(Animal):
    def make_sound(self):
        return "Meow!"

# Creating instances of the derived classes
dog_instance = Dog("Buddy")
cat_instance = Cat("Whiskers")

# Accessing attributes and invoking methods
print(dog_instance.name)  # Output: Buddy
print(dog_instance.make_sound())  # Output: Woof!

print(cat_instance.name)  # Output: Whiskers
print(cat_instance.make_sound())  # Output: Meow!

In the example above, the base class 'Animal' has a common attribute 'name' and a method 'make_sound', which is defined with a 'pass' statement as it is meant to be overridden by the derived classes. The derived classes 'Dog' and 'Cat' inherit from the 'Animal' class and provide their own implementation of the 'make_sound' method.

The program creates instances of 'Dog' and 'Cat', demonstrating how they inherit attributes and methods from the 'Animal' class.

Output:

Buddy
Woof!
Whiskers
Meow!

2.
Explain the concept of a superclass and a subclass.

In object-oriented programming (OOP), a superclass (or base class) is a class that is extended or inherited by one or more other classes, known as subclasses (or derived classes). The superclass contains common attributes and methods shared by its subclasses, promoting code reuse and establishing a hierarchical relationship among classes.

Let's consider an example to illustrate the concepts of a superclass and a subclass in Python:

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

    def make_sound(self):
        return "Generic animal sound"

class Dog(Animal):
    def make_sound(self):
        return "Woof!"

class Cat(Animal):
    def make_sound(self):
        return "Meow!"

# Creating instances of the derived classes
dog_instance = Dog("Buddy")
cat_instance = Cat("Whiskers")

# Accessing attributes and invoking methods
animal_instance = Animal("Generic Animal")
print(animal_instance.name)  # Output: Generic Animal
print(animal_instance.make_sound())  # Output: Generic animal sound

print(dog_instance.name)  # Output: Buddy
print(dog_instance.make_sound())  # Output: Woof!

print(cat_instance.name)  # Output: Whiskers
print(cat_instance.make_sound())  # Output: Meow!

In the example above, 'Animal' is the superclass, which contains a common attribute 'name' and a method 'make_sound'. 'Dog' and 'Cat' are subclasses that inherit from the 'Animal' superclass. Each subclass provides its own implementation of the 'make_sound' method.

The program demonstrates creating instances of the superclass and subclasses, showcasing how attributes and methods are inherited and overridden in the hierarchy.

Output:

Generic Animal
Generic animal sound
Buddy
Woof!
Whiskers
Meow!

3.
How is inheritance related to code reuse in object-oriented programming?

Inheritance is a crucial concept in object-oriented programming (OOP) that facilitates code reuse by allowing a class (subclass/derived class) to inherit attributes and methods from another class (superclass/base class). This enables the subclass to leverage and build upon the functionality defined in the superclass, promoting a more modular and maintainable codebase.

Let's consider an example to illustrate how inheritance contributes to code reuse in Python:

class Shape:
    def __init__(self, color):
        self.color = color

    def draw(self):
        return f"Drawing a {self.color} shape"

class Circle(Shape):
    def __init__(self, color, radius):
        super().__init__(color)
        self.radius = radius

    def draw(self):
        return f"Drawing a {self.color} circle with radius {self.radius}"

class Square(Shape):
    def __init__(self, color, side_length):
        super().__init__(color)
        self.side_length = side_length

    def draw(self):
        return f"Drawing a {self.color} square with side length {self.side_length}"

# Creating instances of the derived classes
circle_instance = Circle("Red", 5)
square_instance = Square("Blue", 4)

# Accessing attributes and invoking methods
print(circle_instance.draw())  # Output: Drawing a Red circle with radius 5
print(square_instance.draw())  # Output: Drawing a Blue square with side length 4

In this example, 'Shape' is the superclass, and 'Circle' and 'Square' are subclasses. The 'Circle' and 'Square' classes inherit the 'draw' method from the 'Shape' superclass, and they provide their own implementations, enhancing or customizing the behavior as needed.

The program demonstrates creating instances of the derived classes, showcasing how the common functionality defined in the 'Shape' superclass is reused by the subclasses. This promotes a more efficient and maintainable codebase by avoiding redundant code for shared features.

Output:

Drawing a Red circle with radius 5
Drawing a Blue square with side length 4

4.
Discuss the syntax for defining a subclass in Python.

In Python, the syntax for defining a subclass involves using the 'class' keyword, specifying the name of the subclass, and indicating the superclass (or base class) from which it inherits. The syntax follows the pattern:

class SubclassName(SuperclassName):
    # Subclass-specific attributes and methods

Let's consider an example to illustrate the syntax for defining a subclass in Python:


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

    def make_sound(self):
        return "Generic animal sound"

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

    def make_sound(self):
        return "Woof!"

# Creating an instance of the subclass
dog_instance = Dog("Buddy", "Labrador")

# Accessing attributes and invoking methods
print(dog_instance.name)  # Output: Buddy
print(dog_instance.breed)  # Output: Labrador
print(dog_instance.make_sound())  # Output: Woof!

In this example, the 'Dog' class is a subclass of the 'Animal' superclass. The syntax for defining the 'Dog' subclass includes the 'class' keyword, the name 'Dog', and the superclass 'Animal' in parentheses. The subclass-specific attributes and methods are then defined within the class body.

The program demonstrates creating an instance of the 'Dog' subclass and accessing its attributes and methods. The use of 'super()' in the subclass's constructor allows the subclass to initialize both its specific attributes and the attributes inherited from the superclass.

Output:

Buddy
Labrador
Woof!