University of Management & Technology
Department of Artificial Intelligence
Lab Journal 06
(Spring 2026)
Course: Programming for Artificial Intelligence Date:
Course Code: AI2101L Faculty: Sameer Ahmed
Objective
To understand and implement the first three types of inheritance in Python: Single, Multiple,
and Multilevel inheritance.
Introduction
Inheritance is a feature of Object-Oriented Programming (OOP) that allows one class to
acquire the properties and methods of another class.
Parent Class: The class whose properties are inherited
Child Class: The class that inherits from the parent
Why Use Inheritance
Code reusability
Easy maintenance
Logical structure of programs
Extensibility
Structure of Inheritance
class Parent:
# parent class code
class Child(Parent):
# child class code
Type 1: Single Inheritance
Definition
Single inheritance occurs when one child class inherits from one parent class.
Example 1
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def bark(self):
print("Dog barks")
d = Dog()
[Link]()
[Link]()
Example 2
class Vehicle:
def start(self):
print("Vehicle starts")
class Car(Vehicle):
def drive(self):
print("Car is driving")
c = Car()
[Link]()
[Link]()
Explanation
The child class uses parent methods and can also define its own methods.
Type 2: Multiple Inheritance
Definition
Multiple inheritance occurs when one child class inherits from more than one parent class.
Example 1
class Father:
def skill1(self):
print("Driving")
class Mother:
def skill2(self):
print("Cooking")
class Child(Father, Mother):
def show(self):
print("Child inherits both skills")
c = Child()
c.skill1()
c.skill2()
[Link]()
Example 2
class Writer:
def write(self):
print("Writing")
class Speaker:
def speak(self):
print("Speaking")
class Person(Writer, Speaker):
pass
p = Person()
[Link]()
[Link]()
Explanation
The child class can access methods from all parent classes.
Type 3: Multilevel Inheritance
Definition
Multilevel inheritance occurs when a class inherits from another class, which itself inherits
from another class.
Example 1
class Grandparent:
def show1(self):
print("Grandparent")
class Parent(Grandparent):
def show2(self):
print("Parent")
class Child(Parent):
def show3(self):
print("Child")
c = Child()
c.show1()
c.show2()
c.show3()
Example 2
class Person:
def info(self):
print("This is a person")
class Employee(Person):
def job(self):
print("This person works in a company")
class Manager(Employee):
def manage(self):
print("This person manages a team")
m = Manager()
[Link]()
[Link]()
[Link]()
Explanation
Inheritance flows in a chain, allowing the final class to access all previous class methods.
Conclusion
The first three types of inheritance—Single, Multiple, and Multilevel—are essential for
writing efficient and reusable code in Python. They help in building structured and scalable
programs.
Lab Tasks
1. Single Inheritance Question
Scenario:
A bank manages customer accounts. Every account has basic features like deposit, withdraw,
show details, and delete account. A savings account also provides interest.
Question:
Design a Python program using single inheritance where:
Requirements:
Create a class BankAccount with:
o name
o balance
o methods:
add_money(amount)
withdraw_money(amount)
show_account()
delete_account()
Create a child class SavingsAccount that:
o inherits BankAccount
o adds a method add_interest() (5% interest on balance)
Task:
Write a Python program demonstrating single inheritance for this bank system.
2. Multiple Inheritance Question
Scenario:
A bank provides different services. Some accounts can use ATM services and loan services
together.
Question:
Design a Python program using multiple inheritance where:
Requirements:
Create a class LoanService with:
o loan(amount) method
Create a class ATMService with:
o withdraw(amount) method
Create a class PremiumAccount that:
o inherits from both LoanService and ATMService
o has:
name
balance
o methods:
add_money(amount)
show_account()
delete_account()
Task:
Write a Python program demonstrating multiple inheritance in a banking system.