[go: up one dir, main page]

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

Practical 15

The document is a lab manual for programming with Python, focusing on practical exercises related to inheritance in object-oriented programming. It outlines the necessary resources, explains the concept and types of inheritance, and provides code examples for creating classes and implementing inheritance. The manual includes tasks for creating employee and student classes, as well as examples of multiple inheritance.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Practical 15

The document is a lab manual for programming with Python, focusing on practical exercises related to inheritance in object-oriented programming. It outlines the necessary resources, explains the concept and types of inheritance, and provides code examples for creating classes and implementing inheritance. The manual includes tasks for creating employee and student classes, as well as examples of multiple inheritance.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Programming with ‘Python’ Practical: 15 Lab Manual (Solved)

Sr. Remark if
Name of Resource Broad Specification Quantity
No. any
1 Computer System Intel i3, 4 GB RAM
For All
2 Operating System Windows/Linux 20
Experiments
3 Development Software Python IDE And VS Code

1. State the use of inheritance


Inheritance is a key Object-Oriented Programming (OOP) concept that allows a class to
inherit properties and methods from another class. It helps in code reusability, modularity, and
maintaining hierarchy in a program.
Main Uses of Inheritance:
1. Code Reusability:
o Common attributes and methods are defined in a base class and reused in derived
classes, reducing code duplication.
2. Method Overriding:
o A subclass can redefine a method from the parent class to provide specialized
behavior.
3. Extensibility:
o New functionalities can be added to existing classes without modifying their code.
4. Data Abstraction and Encapsulation:
o Inheritance helps implement abstraction by creating a base class with generalized
features and extending it for specific use cases.
5. Hierarchical Structure:
o It helps in creating a structured and organized program by defining relationships
between classes.
2. List different types of inheritance
Types of Inheritance in Python
1. Single Inheritance: A subclass inherits from a single parent class.
Example:
class Animal:
def sound(self):
print("Animals make sounds.")

Jamia Polytechnic Akkalkuwa Page No:01 Prepared by: Sayyed Waliullah


Programming with ‘Python’ Practical: 15 Lab Manual (Solved)

class Dog(Animal):
def sound(self):
print("Dog barks.")

d = Dog()
d.sound() # Output: Dog barks.

2. Multiple Inheritance: A subclass inherits from multiple parent classes.


3. Multilevel Inheritance: A subclass inherits from another subclass, forming a chain.
4. Hierarchical Inheritance: Multiple subclasses inherit from a single parent class.
5. Hybrid Inheritance: A combination of two or more types of inheritance.

1. Create a class Employee with data members: name, department and salary. Create suitable
methods for reading and printing employee information
class Employee:
def __init__(self, name, department, salary):
self.name = name
self.department = department
self.salary = salary

def display(self):
print("Employee Name:", self.name)
print("Department:", self.department)
print("Salary:", self.salary)

# Creating an Employee object


emp = Employee("Rashid", "IT", 50000)

# Displaying employee details


emp.display()

2. Python program to read and print students information using two classes using simple
inheritance.
class Person:
def __init__(self, name, age):
self.name = name

Jamia Polytechnic Akkalkuwa Page No:02 Prepared by: Sayyed Waliullah


Programming with ‘Python’ Practical: 15 Lab Manual (Solved)

self.age = age
def display(self):
print("Name:", self.name)
print("Age:", self.age)
class Student(Person):
def __init__(self, name, age, course):
super().__init__(name, age)
self.course = course
def display(self):
super().display()
print("Course:", self.course)

# Creating a Student object


s = Student("Zaid", 20, "Computer Science")
# Displaying student details
s.display()

3. Write a Python program to implement multiple inheritance


class Father: class Teacher:
def show_father(self): def teach(self):
print("I am the Father.") print("I teach students.")

class Mother: class Researcher:


def show_mother(self): def research(self):
print("I am the Mother.") print("I conduct research.")

class Child(Father, Mother): class Professor(Teacher, Researcher):


def show_child(self): def guide(self):
print("I am the Child.") print("I guide students and researchers.")

# Creating an object of Child class # Creating an object of Professor


c = Child() prof = Professor()
# Calling methods from both parent classes
c.show_father() # Calling methods from both parent classes
c.show_mother() prof.teach() # From Teacher class
c.show_child() prof.research() # From Researcher class
prof.guide() # From Professor class

Write Any one Example but perform both

Jamia Polytechnic Akkalkuwa Page No:03 Prepared by: Sayyed Waliullah

You might also like