# 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()
| 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. |
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
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())