0% found this document useful (0 votes)
2 views7 pages

Inheritance in Python

The document is a lab journal for a course on Programming for Artificial Intelligence, focusing on the implementation of inheritance in Python. It covers three types of inheritance: Single, Multiple, and Multilevel, providing definitions, examples, and explanations for each. Additionally, it includes lab tasks requiring the design of Python programs to demonstrate single and multiple inheritance in a banking system.

Uploaded by

Emi Rana
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

Inheritance in Python

The document is a lab journal for a course on Programming for Artificial Intelligence, focusing on the implementation of inheritance in Python. It covers three types of inheritance: Single, Multiple, and Multilevel, providing definitions, examples, and explanations for each. Additionally, it includes lab tasks requiring the design of Python programs to demonstrate single and multiple inheritance in a banking system.

Uploaded by

Emi Rana
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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.

You might also like