0% found this document useful (0 votes)
9 views2 pages

Exp 10

This document outlines an experiment to create a class hierarchy in Python using object-oriented programming principles, specifically focusing on inheritance. It describes the Employee, Manager, and Executive classes, detailing their attributes and methods, including the use of the super() function for initializing inherited attributes. The experiment successfully demonstrates the implementation of these concepts through a practical example and concludes with a validation of the inheritance mechanism.

Uploaded by

Deepanshu Fulara
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)
9 views2 pages

Exp 10

This document outlines an experiment to create a class hierarchy in Python using object-oriented programming principles, specifically focusing on inheritance. It describes the Employee, Manager, and Executive classes, detailing their attributes and methods, including the use of the super() function for initializing inherited attributes. The experiment successfully demonstrates the implementation of these concepts through a practical example and concludes with a validation of the inheritance mechanism.

Uploaded by

Deepanshu Fulara
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/ 2

EXPERIMENT - 10

Aim: Make a class Employee with a name and salary. Make a class Manager inherit
from Employee. Add an instance variable, named department. Write a method that prints the
manager's name, department, and salary. Make a class Executive inherit from Manager. Write
a method that prints the string "Executive" followed by the information stored in
the Manager superclass object.
Software Used: Spyder
Theory:
In object-oriented programming (OOP), classes and objects are used to model real-world
entities and their behaviors. The concept of inheritance and the use of the super() function to
call the parent class's __init__ method are fundamental in organizing and extending code
efficiently.
Inheritance: Inheritance is a mechanism in OOP that allows a new class (called a subclass or
child class) to inherit the properties and behaviors (attributes and methods) of an existing class
(called a superclass or parent class). This allows for code reuse, where a subclass can extend
or modify the behavior of the parent class without rewriting the same code.
The super() Function: The super() function in Python is used to call methods from a parent
class. Specifically, super() allows us to call the parent class's __init__() method to initialize the
inherited attributes.
In this experiment:
1. The Employee class is the base class that holds the basic information (e.g., name and
salary) of an employee.
2. The Manager class inherits from the Employee class and adds additional information
about the department. It also has a method (output1()) that displays manager-specific
information.
3. The Executive class inherits from Manager, which already inherits from Employee.
The Executive class can further extend or modify the Manager behavior, and it has its
own method (output2()) to display executive-specific information.
Source Code:
class Employee:
# Create a method __init__ to initialize name and salary of the employee
def __init__(self, name, salary):
self.name_of_the_employee = name
self.salary_of_the_employee = salary

# Make another subclass Manager


class Manager(Employee):
def __init__(self, name, salary, department):
super().__init__(name, salary)
self.department = department
# Create a method output1 to display manager's name, department, and salary
def output1(self):
print(f"Manager's Name: {self.name_of_the_employee}, Department:
{self.department}, Salary: {self.salary_of_the_employee}")

class Executive(Manager):
def __init__(self, name, salary, department):
super().__init__(name, salary, department)

# Create a method output2 to display executive's name, department, and salary


def output2(self):
print(f"Executive: {self.name_of_the_employee}, Department: {self.department},
Salary: {self.salary_of_the_employee}")

# Call the methods to display output


executive1 = Executive("Deepanshu", 1200000, "Marketing")
executive1.output1()
executive1.output2()

Output:

Conclusion:
In this experiment, I have tested the concept of inheritance and the use of the super() function
in object-oriented programming by creating a class hierarchy consisting of Employee Manager,
and Executive. By utilizing inheritance, I ensured that attributes and methods from the parent
classes were properly inherited by the child classes. The program successfully demonstrated
the use of the super() function to call parent class constructors and methods, ensuring proper
initialization of attributes across multiple levels of inheritance.

You might also like