Python: Inheritance
Session No.: 41
Course Name: Introduction to Python Programming
Course Code: G2UD301B
Instructor Name: Dr. Anil Kumar (Ph.D. MNNIT, Allahabad)
Duration: 0:50 Hrs.
1
Learning Outcomes:
By the end of this session, students will be able to:
Explain the concept of inheritance in Python.
· Implement single and multilevel inheritance.
· Use super() to access parent class members.
Warm--Up (Think–
Warm (Think–Pair
Pair–
–Share) 0 – 5 min
Instructor Prompt:
◦ “If two classes share common properties and behaviors, how can we
avoid code duplication?”
Student Task:
◦ Individually write a short answer.
◦ Share ideas verbally.
Expected Response:
◦ Reusing code
◦ Parent–child relationship
◦ Base and derived classes
Inheritance
Python inheritance allows a new class (child or derived class) to inherit attributes and
methods from an existing class (parent or base class).
This mechanism promotes code reusability, modularity, and the creation of logical
hierarchies.
Key Concepts
Parent Class (Base or Superclass): The class being inherited from, containing
common functionalities.
Child Class (Derived or Subclass): The class that inherits from the parent class. It can
use inherited methods and attributes while also defining its own unique ones.
is-a relationship: Inheritance models this relationship; for example, a Dog "is a" type
of Animal.
Method Overriding: A child class can provide a specific implementation of a method
that is already defined in its parent class.
super() function: This built-in function provides a clean way to call methods of the
parent class from within the child class, especially useful in the __init__() method or
when extending functionality.
Inheritance
Example: Here, a parent class Animal that has a method info(), and a child classes
Dog that inherit from Animal and add their own behaviour.
class Animal: #Parent class
def __init__(self, name):
Output:
[Link] = name Animal name: Buddy
def info(self): Buddy barks
print("Animal name:", [Link]) Explanation:
class Dog(Animal): #child class class Animal: Defines the parent class.
info(): Prints the name of the animal.
def sound(self):
class Dog(Animal): Defines Dog as a child
print([Link], "barks“) of Animal class.
d = Dog("Buddy") #child object [Link](): Calls parent method info()
[Link]() # Inherited method and [Link](): Calls child method.
[Link]()
super() Function
super() function is used to call the parent class’s methods. In particular, it is commonly used in the
child class’s __init__() method to initialize inherited attributes. This way, the child class can leverage
the functionality of the parent class.
Example: Here, Dog uses super() to call Animal’s constructor
class Animal: # Parent Class: Animal
def __init__(self, name):
Output:
[Link] = name
def info(self): Animal name: Buddy
print("Animal name:", [Link]) Buddy is a Golden Retriever
class Dog(Animal): # Child Class: Dog Explanation:
def __init__(self, name, breed): •The super() function is used inside
super().__init__(name) # Call parent constructor __init__() method of Dog to call the
[Link] = breed constructor of Animal and initialize
def details(self): inherited attribute (name).
print([Link], "is a", [Link]) •This ensures that parent class functionality
d = Dog("Buddy", "Golden Retriever") is reused without needing to rewrite the
[Link]() # Parent method code in the child class.
[Link]() # Child method
Types of Python Inheritance
1. Single Inheritance:
In single inheritance, a child class inherits from just one parent class.
Example: This example shows a child class Employee inheriting a property from
the parent class Person.
class Person:
def __init__(self, name): Output:
[Link] = name Name: Sarah
class Employee(Person): Sarah is an employee
# Employee inherits from Person Explanation:
def show_role(self): Here Employee inherits name from Person,
print([Link], "is an employee") it also defines its own method
emp = Employee("Sarah") show_role().
print("Name:", [Link])
emp.show_role()
Types of Python Inheritance
2. Multiple Inheritance
In multiple inheritance, a child class can inherit from more than one parent class.
Example: This example demonstrates Employee inheriting properties from two parent classes: Person
and Job. # Inherits from both Person and Job
class Person:
def __init__(self, name):
[Link] = name
class Job: Output:
def __init__(self, salary):
Jennifer earns 50000
[Link] = salary
class Employee(Person, Job): Explanation: Here Employee gets
def __init__(self, name, salary): attributes from both Person and Job and It
Person.__init__(self, name) can access both name and salary.
Job.__init__(self, salary)
def details(self):
print([Link], "earns", [Link])
emp = Employee("Jennifer", 50000)
[Link]()
Types of Python Inheritance
3. Multilevel Inheritance
In multilevel inheritance, a class is derived from another derived class (like a chain).
Example: This example shows Manager inheriting from Employee, which in turn inherits from Person.
#Person ->Employee ->Manager
class Person: Output:
def __init__(self, name): Joy is an employee
[Link] = name Joy manages HR department
class Employee(Person): Explanation: Here Manager inherits from
def show_role(self): Employee and Employee inherits from
print([Link], "is an employee") Person. So Manager can use methods from
class Manager(Employee): both parent and grandparent.
def department(self, dept):
print([Link], "manages", dept, "department")
mgr = Manager("Joy")
mgr.show_role()
[Link]("HR")
Types of Python Inheritance
4. Hierarchical Inheritance
In hierarchical inheritance, multiple child classes inherit from the same parent class.
Example: This example demonstrates two child classes (Employee and Intern) inheriting from a single
parent class Person.
class Person: Output:
def __init__(self, name): David works as an employee
[Link] = name Eva is an intern
class Employee(Person): Explanation: Both Employee and Intern
def role(self): inherit from Person. They share the
print([Link], "works as an employee") parent’s property (name) but implement
class Intern(Person):
their own methods.
def role(self):
print([Link], "is an intern")
emp = Employee("David")
[Link]()
intern = Intern("Eva")
[Link]()
Types of Python Inheritance
5. Hybrid Inheritance: Hybrid inheritance is a combination of more than one type of inheritance.
Example: This example demonstrates TeamLead inheriting from both Employee (which inherits Person) and Project,
combining multiple inheritance types.
class Person:
def __init__(self, name): Output:
[Link] = name
class Employee(Person):
Sophia is an employee
def role(self): Sophia leads project: AI Development
print([Link], "is an employee") Explanation: Here TeamLead inherits from
class Project:
def __init__(self, project_name):
Employee (which already inherits Person) and
self.project_name = project_name also from Project.
class TeamLead(Employee, Project): # Hybrid Inheritance This combines single, multilevel and multiple
def __init__(self, name, project_name):
Employee.__init__(self, name)
inheritance -> hybrid.
Project.__init__(self, project_name)
def details(self):
print([Link], "leads project:", self.project_name)
lead = TeamLead("Sophia", "AI Development")
[Link]()
[Link]()
Think – Pair – Share (15 min)
Task: class Vehicle:
Task: def start(self):
Create: print("Vehicle started")
Base class:Vehicle → method start()
Derived class: Car → method drive() class Car(Vehicle):
def drive(self):
Expected Output: print("Car is driving")
Vehicle started
Car is driving c = Car()
[Link]()
[Link]()
Debugging and Correct code (10 minutes)
class Employee:
def __init__(self, name, salary): Task:
[Link] = name Identify the error
Fix the code
[Link] = salary class Manager(Employee):
class Manager(Employee): def __init__(self, name, salary, dept):
def __init__(self, dept): super().__init__(name, salary)
[Link] = dept
[Link] = dept
m = Manager("HR")
print([Link])
Wrap--Up & Reflection (5 minutes)
Wrap
What is inheritance in Python?
Why do we use super()?
What happens when a child class overrides a parent
method?
Quick Summary:
Inheritance enables code reuse
Child class can extend or modify parent behavior
super() avoids duplication and improves maintainability
Next Session:
Database connection
THANK YOU !