- Home
- Course Detail
regularpython@gmail.com
You are now watching:
Python Basics in English / of Inheritance Class
Python Inheritance – Concept, Types, Overriding & super()
Easy explanation for classroom teaching and interview preparation
1. What is Inheritance in Python?
Inheritance is a feature in Object-Oriented Programming where one class (Child / Derived Class) can reuse the properties and methods of another class (Parent / Base Class).
•
Goal: Reuse existing code and avoid duplication.
•
We write common logic in the parent class and special logic in the child class.
Simple Real-Time Example
Think of a general Employee class and a more specific Developer class. Developer is also an Employee, so it can reuse Employee details (name, id, salary) and add its own behaviour (programming language, project, etc.).
# Base (Parent) class
class Employee:
def __init__(self, name, emp_id):
self.name = name
self.emp_id = emp_id
def show_details(self):
print(f"Name: {self.name}, Emp ID: {self.emp_id}")
# Derived (Child) class
class Developer(Employee):
def __init__(self, name, emp_id, tech):
super().__init__(name, emp_id) # reuse Employee __init__
self.tech = tech
def show_tech(self):
print(f"Working on: {self.tech}")
# Usage
dev = Developer("Sai", 101, "Python")
dev.show_details()
dev.show_tech()
2. Types of Inheritance in Python
➤
Below are the common inheritance types with simple explanations and examples.
| Type | Description | Small Real-Time Example Idea |
|---|---|---|
| Single Inheritance | One child class inherits from exactly one parent class. | Employee → Developer Developer inherits from only Employee. |
| Multilevel Inheritance | A chain of inheritance: Class C inherits from B, and B inherits from A. | Person → Employee → Manager Manager is also an Employee and also a Person. |
| Multiple Inheritance | Child class inherits from more than one parent class. | TeamLead(Developer, Tester) TeamLead has features from both Developer and Tester. |
| Hierarchical Inheritance | One parent class with multiple child classes. | Employee → Developer, Tester, HR All are different roles, but all are Employees. |
| Hybrid Inheritance | Any combination of above types (e.g., multilevel + multiple). | Person → Employee and Employee → (Developer, Manager) with some classes using multiple inheritance. |
3. Method Overriding
Method Overriding happens when the child class defines a method with the same name and same parameters as the parent class, but with a different implementation.
✔
Used to change or customize the parent class behaviour in the child class.
✔
Same method name, different logic, based on the object type.
Real-Time Example – Different Login Methods
Think about a generic User and a specific AdminUser. Both have a login() method, but AdminUser may have extra security checks.
class User:
def login(self):
print("Normal user login")
class AdminUser(User):
def login(self): # overriding
print("Admin login with extra security check")
# Usage
u = User()
a = AdminUser()
u.login() # Output: Normal user login
a.login() # Output: Admin login with extra security check
★
At runtime, Python decides which method to call based on the object type (User or AdminUser). This is called runtime polymorphism.
4. super() in Python
The super() function is used in the child class to call methods of the parent class.
•
Commonly used inside __init__ to reuse parent initialization.
•
Also useful to call parent class methods when we override them.
Real-Time Example – Salary Calculation
Base class calculates basic salary, child class adds bonus on top of it.
class Employee:
def __init__(self, name, base_salary):
self.name = name
self.base_salary = base_salary
def get_salary(self):
return self.base_salary
class Manager(Employee):
def __init__(self, name, base_salary, bonus):
super().__init__(name, base_salary) # call Employee __init__
self.bonus = bonus
def get_salary(self): # overriding
base = super().get_salary() # call Employee get_salary
return base + self.bonus
# Usage
mgr = Manager("Sai", 50000, 15000)
print("Total Salary:", mgr.get_salary())
💡
Using super() helps us reuse parent logic and then extend it in child class.
5. Quick Summary
1.
Inheritance allows one class to use properties and methods of another class.
2.
Common types: Single, Multilevel, Multiple, Hierarchical, Hybrid.
3.
Method Overriding lets child classes change parent behaviour.
4.
super() is used to call parent methods from child class and reuse logic.