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 SavingsAccountis aBankAccount, a Dogis anAnimal.
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
Type
Structure
Example
Single
One child, one parent
class B(A)
Multilevel
A chain: grandparent → parent → child
class B(A), class C(B)
Multiple
One child with two or more parents
class C(A, B)
Hierarchical
Several children of one parent
class B(A), class C(A)
Hybrid
A combination of the above
class D(B, C) where B and C both inherit A
Multilevel and multiple inheritance
Multilevel: A ──► B ──► C C has everything from B and AMultiple: 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 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”.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-201/classes-in-python/inheritance/ to read this page.