Exp 10
Exp 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
class Executive(Manager):
def __init__(self, name, salary, department):
super().__init__(name, salary, department)
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.