MCS-201 · Unit 13

Classes in Python

Section 3 of 7

Block 4 · Unit 13Classes in Python11 min · Section 3 of 7

13.3Inheritance

Covers IGNOU MCS-201 §13.4

Inheritance lets a new class reuse and extend the code of an existing class instead of copying it. It models an “is-a” relationship: a SavingsAccount is a BankAccount, a Dog is an Animal.

Parent and child classes

Syntax
class Child(Parent):
    additional or redefined attributes and methods

The child can use everything the parent defines, add new methods, and override (redefine) parent methods. The parent knows nothing about its children. Every class ultimately inherits from the built-in class object.

Types of inheritance

TypeStructureExample
SingleOne child, one parentclass B(A)
MultilevelA chain: grandparent → parent → childclass B(A), class C(B)
MultipleOne child with two or more parentsclass C(A, B)
HierarchicalSeveral children of one parentclass B(A), class C(A)
HybridA combination of the aboveclass D(B, C) where B and C both inherit A
Multilevel and multiple inheritance
Multilevel:   A ──► B ──► C        C has everything from B and A

Multiple:     A     B
               \   /
                 C                 C has everything from A and B

Constructors and super()

  • If the child defines no __init__, the parent’s __init__ is used.
  • If the child defines __init__, it replaces the parent’s; the parent’s constructor does not run unless the child calls it.
  • super().__init__(...) calls the parent’s version, so the parent can set up its own attributes. super() works for any method, not just constructors.

Multiple inheritance and the MRO

When a class has several parents, Python searches for a method in a fixed order called the method resolution order (MRO): the class itself, then its parents from left to right, making sure each class comes before its own parents and appears only once. ClassName.__mro__ (or ClassName.mro()) shows it.

The diamond: D(B, C), with B(A) and C(A)
        A
       / \
      B   C
       \ /
        D          MRO: D → B → C → A → object

super() follows the MRO, not simply “the parent”. When every class in the diamond calls super().__init__(), each constructor runs exactly once, in MRO order.

Key takeaways

  • class Child(Parent): inherits everything from Parent.
  • Types: single, multilevel, multiple, hierarchical, hybrid.
  • A child’s __init__ replaces the parent’s; call super().__init__() to run both.
  • Multiple inheritance follows the MRO (Class.__mro__).

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

Which statement creates a class Dog that inherits from Animal?

Show answer

Answer: C. class Dog(Animal):

Parent classes are listed in parentheses after the child’s name. extends is Java syntax.

Question 2 · multiple choice

What type of inheritance is class B(A) followed by class C(B)?

Show answer

Answer: A. Multilevel

It forms a chain A → B → C. Multiple inheritance would be one class with several parents.

Question 3 · multiple choice

What does this program print?

Python · quiz_super.py
class P:
    def __init__(self):
        print("P")

class Q(P):
    def __init__(self):
        print("Q")

Q()
Show answer

Answer: C. Q

Q’s constructor replaces P’s and does not call super().__init__(), so only “Q” is printed.

Question 4 · multiple choice

For class D(B, C) where both B and C inherit from A, what is D.__mro__ (by class name)?

Show answer

Answer: D. D, B, C, A, object

Parents are searched left to right, and A comes after both B and C because it is their shared parent.

Question 5 · multiple choice

What does this program print?

Python · quiz_override.py
class Shape:
    def name(self):
        return "shape"
    def show(self):
        return "I am a " + self.name()

class Square(Shape):
    def name(self):
        return "square"

print(Square().show(), Shape().show())
Show answer

Answer: B. I am a square I am a shape

show is inherited, but self.name() calls the method of the actual object, so a Square reports “square”.

Question 6 · short answer

Write a class Person with name and a method greet(), and a class Student(Person) that adds roll_no, calls the parent constructor with super(), and overrides greet() to also print the roll number.

Show worked answer

class Person: with def __init__(self, name): self.name = name and def greet(self): print("Hello,", self.name). Then class Student(Person): with def __init__(self, name, roll_no): super().__init__(name); self.roll_no = roll_no and def greet(self): super().greet(); print("Roll no:", self.roll_no). Student("Asha", 101).greet() prints “Hello, Asha” and “Roll no: 101”.

↑ ↓ move↵ openTab switch type