Python Inheritance

In Python, inheritance is an essential part of Object-Oriented Programming (OOP). It lets a class, known as a child or subclass, inherit attributes and methods from another class, called a parent or superclass. This builds on existing code, promotes code reuse, and creates a clear class structure.

Benefits of Inheritance

Code Reusability: You can reuse existing code without rewriting it.

Hierarchical Class Structure: It creates a clear class hierarchy and logical structure.

Modularity and Extensibility: You can easily extend the functionality of existing classes.

Basic Inheritance

Inheritance allows the subclass to use the properties and methods of the superclass. The subclass can also override methods and add new attributes or methods.

Example:


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

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

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

# Creating an instance of Dog
my_dog = Dog("Tommy")
print(my_dog.speak())  # Output: Tommy barks

In this example:

Animal is the parent class, with a method speak.

Dog is a child class that inherits from Animal and overrides the speak method.

Types of Inheritance

Single Inheritance

A child class inherits from only one parent class.


class Parent:
    pass

class Child(Parent):
    pass

Multiple Inheritance

A child class inherits from more than one parent class.


class Parent1:
    pass

class Parent2:
    pass

class Child(Parent1, Parent2):
    pass

Multilevel Inheritance

A chain of inheritance occurs when a class is derived from another derived class.


class Grandparent:
    pass

class Parent(Grandparent):
    pass

class Child(Parent):
    pass

Using super() to Access the Parent Class

The super() function allows a subclass to call a method or constructor from its parent class. This is helpful when you override methods in the subclass but still want to use the parent’s features.


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

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

class Cat(Animal):
    def __init__(self, name, breed):
        super().__init__(name)  # Call the parent constructor
        self.breed = breed

    def speak(self):
        return f"{self.name} meows"

# Creating an instance of Cat
my_cat = Cat("Whiskers", "Siamese")
print(my_cat.speak())         # Output: Whiskers meows
print(my_cat.breed)           # Output: Siamese

Method Overriding

In a child class, you can override a method from the parent class by defining it with the same name. This helps customize the inherited behavior.


class Animal:
    def speak(self):
        return "Some generic sound"

class Dog(Animal):
    def speak(self):
        return "Bark"

my_dog = Dog()
print(my_dog.speak())  # Output: Bark

Python Inheritance – Questions and Answers

Q 1: What is inheritance in Python?

Ans: Inheritance allows a class to acquire attributes and methods from another class.

Q 2: What is a parent (base) class?

Ans: A class whose attributes and methods are inherited by another class.

Q 3: What is a child (derived) class?

Ans: A class that inherits attributes and methods from a parent class.

Q 4: Can a child class override parent methods?

Ans: Yes, it can redefine methods to change behavior.

Q 5: What types of inheritance does Python support?

Ans: Single, multiple, multilevel, hierarchical, and hybrid inheritance.

Python Inheritance – Objective Questions (MCQs)

Q1. What is inheritance in Python?






Q2. Which of the following is the correct syntax for inheritance in Python?






Q3. What will be the output of the following code?

class A:
def show(self):
print("Class A")
class B(A):
pass
obj = B()
obj.show()






Q4. Which function can be used to call the parent class constructor inside a derived class?






Q5. What type of inheritance is shown below?

class A: pass
class B(A): pass
class C(B): pass






Related Python Inheritance Topics