Python Inheritance
Table of Contents
Description
Inheritance is a fundamental concept in Object-Oriented Programming (OOP) that allows a class (child/derived class) to inherit properties and behavior (methods and variables) from another class (parent/base class).
Python supports:
Single Inheritance
Multiple Inheritance
Multilevel Inheritance
Hierarchical Inheritance
Hybrid Inheritance
Prerequisites
- Understanding of classes and objects
- Basics of methods, variables (instance/class)
- Familiarity with function overriding and method resolution
Examples
Here's a simple program in Python:
✅ Single Inheritance class Animal: def sound(self): print("Animal makes sound") class Dog(Animal): # Inherits from Animal def bark(self): print("Dog barks") d = Dog() d.sound() # Output: Animal makes sound d.bark() # Output: Dog barks ✅ Multilevel Inheritance class Animal: def eat(self): print("Animal eats") class Dog(Animal): def bark(self): print("Dog barks") class Puppy(Dog): def weep(self): print("Puppy weeps") p = Puppy() p.eat() # Output: Animal eats p.bark() # Output: Dog barks p.weep() # Output: Puppy weeps ✅ Multiple Inheritance class Father: def skill(self): print("Father is good at driving") class Mother: def cook(self): print("Mother is good at cooking") class Child(Father, Mother): def play(self): print("Child plays football") c = Child() c.skill() # Output: Father is good at driving c.cook() # Output: Mother is good at cooking ✅ Hierarchical Inheritance class Parent: def show(self): print("This is parent") class Child1(Parent): def c1(self): print("This is child 1") class Child2(Parent): def c2(self): print("This is child 2") ch1 = Child1() ch2 = Child2() ch1.show() # Output: This is parent ch2.show() # Output: This is parent ✅ Hybrid Inheritance class A: def methodA(self): print("Method A") class B(A): def methodB(self): print("Method B") class C(A): def methodC(self): print("Method C") class D(B, C): # Hybrid: Combination of multiple and multilevel def methodD(self): print("Method D") d = D() d.methodA() # Inherited from A ✅ Method Overriding class Vehicle: def start(self): print("Vehicle starting...") class Car(Vehicle): def start(self): # Overrides parent method print("Car starting...") c = Car() c.start() # Output: Car starting... ✅ Using super() to call Parent Methods class A: def show(self): print("Show from A") class B(A): def show(self): super().show() # Calls method from class A print("Show from B") b = B() b.show() # Output: # Show from A # Show from BReal-World Applications
Banking Systems: Base class Account, subclasses like SavingsAccount, LoanAccount
E-commerce: User base class with Customer, Admin, Vendor subclasses
Game Development: Common Character class with Warrior, Mage, etc.
GUI Frameworks: Base Widget class, inherited by Button, Label, TextField
Where topic Can Be Applied
Software architecture design (frameworks, SDKs)
Web development using class-based views (e.g., Django)
Reusable code libraries and modular systems
Scientific computing tools where base functionality is extended
Resources
WatchTopic video source
A comprehensive video
VisitPython pdf
pdf on topic
Interview Questions
What is inheritance and why is it useful?
Explain the types of inheritance in Python.
How does method overriding work?
What is the use of super() in inheritance?
Can Python classes inherit from multiple classes? Explain with an example.
What is the MRO (Method Resolution Order)?