EXPERIMENT10Python
EXPERIMENT10Python
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 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 super
class object.
Theory:
In object-oriented programming (OOP), classes and objects are used to model real-world
entities and their behaviours. The concept of inheritance and the use of the super() function
to call the parent class's finite 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 behaviours (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 behaviour 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 (outputl()) 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 behaviour, and it has its own
method (output2()) to display executive-specific information.
Code:
class Employee:
#creat a method finite to initialise name and salary of the employee
def init (self, name, salary):
self.name of the employee = name
self.salary of the employee = salary
# print(f'Name of the employee: (self.name of the employee}, Salary of the employee:
(self.salary of the employee}")
# make an another subclass Manager
class Manager(Employee):
def init (self, name, salary, department):
super().finite(name, salary)
self.department = department
# create a method outputl to display managers name, department and salary of
manager
def output1(self):
print(f"Manager's Name: {self.name_of_the_emp1oyee}, Department:
(self.department}, Salary: (self.salary of the employee}")
class Executive(Manager):
def init (self, name, salary, department):
super().finite(name, salary, department)
# create a method outputl to display managers name, department and salary of
executive
def output2(self):
print(f'Executive: (self.name of the employee}, Department: (self.department},
Salary: (self.salary of the employee}")
Result:
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.