Explore programming tutorials, exercises, quizzes, and solutions!
Python Inheritance Exercises
1/21
In Python’s object-oriented programming, inheritance allows you to define a new class that reuses, extends, or modifies the behavior of an existing class. The base class (or parent class) contains common logic, while the derived class (or child class) inherits those attributes and methods.
This promotes code reuse, reduces duplication, and enables polymorphic behavior — where objects of different classes can be treated the same if they share a common base.
Consider the following Python code:
class Animal:
def speak(self):
return "Some sound"
class Dog(Animal):
def speak(self):
return "Bark"
d = Dog()
print(d.speak())
What concept does this code illustrate about Python inheritance?
This code demonstrates method overriding in inheritance.
The Animal class defines a method speak() returning a default message.
The Dog class inherits from Animal but overrides the speak() method to return "Bark".
When d = Dog() is created and d.speak() is called, Python executes the child class’s version of the method, not the parent’s.
This is a key feature of inheritance — derived classes can either use inherited behavior or replace it with their own. This allows developers to create class hierarchies where each class adds or customizes behavior while still leveraging shared logic from base classes.
Which of the following statements is correct about inheritance in Python?
Inheritance in Python allows a class (child class) to inherit attributes and methods from another class (parent class). This enables code reuse and extension of existing functionality. Python supports single and multiple inheritance.
What is the output of the following Python code?
class Parent:
def __init__(self):
self.name = "Parent"
class Child(Parent):
def __init__(self):
super().__init__()
self.name = "Child"
child = Child()
print(child.name)
The 'Child' class inherits from 'Parent' and uses 'super().__init__()' to call the parent class's constructor. After that, the 'name' attribute in 'Child' is set to "Child". Hence, the value of 'child.name' is "Child".
Which of the following is true about the 'super()' function in Python inheritance?
The 'super()' function in Python is used to call a method or constructor from the parent class. It is commonly used to invoke the parent class's methods when they are overridden in the child class. This ensures that functionality from the parent class can be reused.
What is the output of the following code?
class A:
def __init__(self):
self.value = 10
class B(A):
pass
obj = B()
print(obj.value)
Class 'B' inherits from class 'A' and does not override the 'value' attribute. Therefore, the object 'obj' created from class 'B' has access to the 'value' attribute defined in class 'A', which is 10. Hence, the output is 10.
What will be the output of the following Python code?
class Vehicle:
def __init__(self, make, model):
self.make = make
self.model = model
class Car(Vehicle):
def __init__(self, make, model, year):
super().__init__(make, model)
self.year = year
car = Car("Toyota", "Corolla", 2021)
print(car.make, car.model, car.year)
The 'Car' class inherits from the 'Vehicle' class. When the 'Car' class object is created, the constructor of the 'Vehicle' class is called via 'super()', and the attributes 'make' and 'model' are initialized. The 'year' is specific to the 'Car' class. Hence, the output is 'Toyota Corolla 2021'.
Which of the following is a feature of inheritance in Python?
Python supports multiple inheritance, meaning a child class can inherit from multiple parent classes. This allows for a class to inherit functionality from more than one parent class, which is a key feature of Python's inheritance system.
What is the output of the following code?
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def speak(self):
print("Dog barks")
dog = Dog()
dog.speak()
The 'Dog' class overrides the 'speak' method of the 'Animal' class. When 'dog.speak()' is called, the overridden method in the 'Dog' class is executed, printing "Dog barks". This is an example of method overriding in inheritance.
Which of the following statements is true about method overriding in Python inheritance?
Method overriding in Python allows the child class to provide its own implementation of a method defined in the parent class. For this, the method in the child class must have the same signature (name and parameters) as the one in the parent class.
What is the output of the following Python code?
class Shape:
def area(self):
return "Area of Shape"
class Circle(Shape):
def area(self):
return "Area of Circle"
class Square(Shape):
def area(self):
return "Area of Square"
shape = Shape()
circle = Circle()
square = Square()
print(shape.area())
print(circle.area())
print(square.area())
The method 'area' is overridden in both 'Circle' and 'Square' classes. When 'shape.area()' is called, the method from the 'Shape' class is used. For the 'circle' and 'square' objects, their respective overridden methods are called, producing the outputs "Area of Circle" and "Area of Square".
What will be the output of the following code?
class Parent:
def __init__(self, name):
self.name = name
def greet(self):
return f"Hello, {self.name}"
class Child(Parent):
def __init__(self, name, age):
super().__init__(name)
self.age = age
def greet(self):
return f"Hello, {self.name}. I am {self.age} years old."
child = Child("Alice", 12)
print(child.greet())
The 'Child' class overrides the 'greet' method of the parent class. When 'child.greet()' is called, the overridden method in the child class is executed, which returns "Hello, Alice. I am 12 years old." The parent method is not called here because it has been overridden in the child class.
What is the output of the following code?
class Animal:
def __init__(self, species):
self.species = species
class Mammal(Animal):
def __init__(self, species, legs):
super().__init__(species)
self.legs = legs
class Dog(Mammal):
def __init__(self, species, legs, breed):
super().__init__(species, legs)
self.breed = breed
dog = Dog("Canine", 4, "Bulldog")
print(dog.species, dog.legs, dog.breed)
The 'Dog' class inherits from 'Mammal', which in turn inherits from 'Animal'. The 'super()' function calls the constructors of the parent classes. The 'Dog' object contains the 'species' ("Canine"), 'legs' (4), and 'breed' ("Bulldog") attributes. These are printed when 'dog.species', 'dog.legs', and 'dog.breed' are accessed.
Which of the following will raise an error in this code?
The 'Child' class correctly overrides the 'display' method of the 'Parent' class. The 'Child' object has access to the 'value' attribute from the parent class, which is multiplied by 2 and printed. Therefore, the output will be 20, and no error occurs.
What will happen if you try to access a parent class method that has been overridden, without using super() in the child class?
If the parent class method is overridden in the child class and 'super()' is not used to explicitly call the parent method, the parent method cannot be accessed directly from the child object. This results in an AttributeError if you try to access the overridden parent method.
What will be the output of the following Python code?
The 'Child' class overrides the 'calculate' method of the 'Parent' class and uses 'super().calculate()' to call the parent class's method. The parent method multiplies 'value' by 2, and then the child method adds 5. The final result is 15 (5 * 2 + 5).
What will be the output of the following code?
class A:
def __init__(self):
print("A init")
class B(A):
def __init__(self):
print("B init")
super().__init__()
class C(B):
def __init__(self):
print("C init")
super().__init__()
obj = C()
The constructor chaining works from child to parent. First, C's init is called, which prints "C init", then calls B's init (prints "B init"), which then calls A's init (prints "A init"). So the correct order is: C init, B init, A init.
What will be printed by the following code?
class A:
def method(self):
print("A")
class B(A):
def method(self):
print("B")
class C(A):
def method(self):
print("C")
class D(B, C):
pass
d = D()
d.method()
In multiple inheritance, Python uses MRO (Method Resolution Order). Since D inherits from B and C, it checks B first, and B overrides A's method with "B". So "B" is printed. C's method is not reached due to MRO order.
Which method will be called when using super() in the following scenario?
class Base:
def greet(self):
print("Hello from Base")
class Derived(Base):
def greet(self):
super().greet()
print("Hello from Derived")
d = Derived()
d.greet()
The method 'greet' in Derived calls 'super().greet()', which triggers the Base class's 'greet' method. After that, it prints its own message. Thus, both lines are printed in order, showing a proper method override with parent method access.
What will be the result of running the following code?
Each class builds on the previous one using super(). X sets x = 1, Y sets y = x + 1 = 2, and Z sets z = y + 1 = 3. All values are stored in the same object and printed successfully: 1 2 3.
What will be the output of the following code?
class A:
def show(self):
print("A.show")
class B(A):
def show(self):
print("B.show")
class C(B):
def show(self):
super(B, self).show()
obj = C()
obj.show()
This tricky use of 'super(B, self)' jumps past B and goes to A in the MRO chain. Even though C is overriding show, the super() call is intentionally skipping B’s method and directly using A’s show method. This is an advanced technique to jump over classes.
What will be the output of the following code?
class A:
def show(self):
print("A.show")
class B(A):
def show(self):
print("B.show")
class C(B):
def show(self):
super(B, self).show()
obj = C()
obj.show()
This tricky use of 'super(B, self)' jumps past B and goes to A in the MRO chain. Even though C is overriding show, the super() call is intentionally skipping B’s method and directly using A’s show method. This is an advanced technique to jump over classes.
Practicing Python Inheritance? Don’t forget to test yourself later in
our
Python Quiz.
About This Exercise: Python – Inheritance
Welcome to the Python Inheritance exercises — a carefully designed set of challenges to help you master one of the most powerful and fundamental concepts in object-oriented programming. Inheritance allows you to create new classes based on existing ones, enabling code reuse, easier maintenance, and logical organization of related classes. Whether you are new to Python or aiming to deepen your object-oriented skills, this section will guide you step-by-step through inheritance principles and their practical applications.
Inheritance lets a child class inherit attributes and methods from a parent class, reducing code duplication and promoting a hierarchical structure in your programs. Through these exercises, you will learn how to define parent and child classes, override methods to customize behavior, and extend functionality without rewriting existing code. You’ll also explore multiple inheritance, where a class inherits from more than one parent, and understand how Python resolves method calls in such complex scenarios.
These exercises will take you from the basics of single inheritance to more advanced topics like using the super() function to call parent class methods, managing method resolution order (MRO), and designing flexible class hierarchies. By practicing these patterns, you’ll build cleaner, more modular, and easier-to-maintain Python applications.
Understanding inheritance is essential for software developers working on real-world projects where code reuse and extensibility are priorities. It’s a core part of designing scalable systems, frameworks, and libraries in Python. Mastering inheritance will also prepare you well for technical interviews, where questions on class relationships and OOP design frequently appear.
In addition to hands-on coding problems, this section emphasizes best practices such as avoiding deep inheritance trees, preferring composition over inheritance when appropriate, and writing clear, intuitive class interfaces. These practices will help you write Python code that is both efficient and maintainable.
We encourage you to combine these exercises with related topics such as classes and objects, polymorphism, and encapsulation to develop a solid object-oriented programming foundation. Using quizzes and MCQs can further reinforce your understanding of inheritance concepts.
Start practicing the Python Inheritance exercises today to unlock the full potential of object-oriented design. With regular practice, you’ll confidently create reusable and extensible Python code, making your projects more professional and scalable.