๐งฌ What is Inheritance?
Inheritance allows one class (child/derived) to acquire the properties and behaviors of another class (parent/base).
It promotes code reusability and hierarchical classification.
๐งช Example of Inheritance
class Animal:
def speak(self):
print("Animals make sounds")
class Dog(Animal):
def speak(self):
print("Dog barks")
class Cat(Animal):
def speak(self):
print("Cat meows")
# Testing
d = Dog()
c = Cat()
d.speak() # Output: Dog barks
c.speak() # Output: Cat meows
๐ฏ Why You Should Learn Inheritance
โ๏ธ Encourages code reuse (DRY principle)
โ๏ธ Helps manage large codebases
โ๏ธ Enhances program structure with logical hierarchies
๐ Use Cases of Inheritance
Scenario |
Description |
Employee → Manager |
Manager inherits common attributes from Employee but adds specific roles |
Vehicle → Car, Bike |
All vehicles have wheels and engine, subclasses add specific behavior |
Shape → Circle, Rectangle |
Each shape shares basic geometric behaviors, but implements them differently |
๐ Real-time Scenarios
๐ฅ In a hospital management system, base class Person can be extended by Patient and Doctor
๐๏ธ In an e-commerce site, base Product class can be extended by Electronics, Clothing, etc.
๐ In a vehicle app, the base class Vehicle can be inherited by Car, Bus, Bike
๐งฉ Core Concepts in Inheritance
No. |
Concept |
Description |
1๏ธโฃ |
Base Class (Parent) |
The class being inherited from. |
2๏ธโฃ |
Derived Class (Child) |
The class that inherits from the parent. |
3๏ธโฃ |
super() Function |
Calls methods of the parent class from the child class. |
4๏ธโฃ |
Method Overriding |
Redefines a parent class method in the child class. |
5๏ธโฃ |
Single Inheritance |
Child inherits from one parent. |
6๏ธโฃ |
Multilevel Inheritance |
Inheritance across multiple levels. |
7๏ธโฃ |
Multiple Inheritance |
Child class inherits from multiple parents. |
8๏ธโฃ |
Hierarchical Inheritance |
Multiple child classes from one parent class. |
9๏ธโฃ |
Hybrid Inheritance |
Mix of more than one type of inheritance. |
๐ |
isinstance() |
Checks if an object is an instance of a class. |
1๏ธโฃ1๏ธโฃ |
issubclass() |
Checks if a class is a subclass of another. |
๐ Practice Tests
โ
Create a base class called Appliance
and extend it with WashingMachine
and Refrigerator
โ
Add a method in base class and override it in child classes
โ
Demonstrate single, multilevel, and multiple inheritance
๐ฅ Real-time Inheritance Example – Hospital System
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def display_info(self):
print(f"Name: {self.name}, Age: {self.age}")
class Doctor(Person):
def __init__(self, name, age, specialization):
super().__init__(name, age)
self.specialization = specialization
def display_info(self):
super().display_info()
print(f"Specialization: {self.specialization}")
class Patient(Person):
def __init__(self, name, age, illness):
super().__init__(name, age)
self.illness = illness
def display_info(self):
super().display_info()
print(f"Illness: {self.illness}")
# Usage
doc = Doctor("Dr. Ramesh", 45, "Cardiologist")
pat = Patient("Sita", 30, "Fever")
# Show details
doc.display_info()
print("-----")
pat.display_info()
๐ 1. Single Inheritance
One child class inherits from one parent class.
class Vehicle:
def start_engine(self):
print("Engine started")
class Car(Vehicle):
def play_music(self):
print("Playing music")
c = Car()
c.start_engine()
c.play_music()
๐ 2. Multilevel Inheritance
A class inherits from a derived class which inherits from another class.
class Person:
def info(self):
print("This is a person")
class Doctor(Person):
def specialization(self):
print("Specialization in medicine")
class Surgeon(Doctor):
def surgery(self):
print("Performs surgery")
s = Surgeon()
s.info()
s.specialization()
s.surgery()
๐ 3. Multiple Inheritance
One class inherits from multiple parent classes.
class Camera:
def take_photo(self):
print("Taking photo")
class Phone:
def make_call(self):
print("Calling...")
class Smartphone(Camera, Phone):
def browse_internet(self):
print("Browsing internet")
s = Smartphone()
s.take_photo()
s.make_call()
s.browse_internet()
โ
Summary Table
๐ท Type |
๐ Definition |
๐ Real-time Example |
Single |
One child class inherits from one parent |
Car inherits from Vehicle |
Multilevel |
A class inherits from a derived class |
Surgeon → Doctor → Person |
Multiple |
A class inherits from multiple parents |
Smartphone → Camera + Phone |