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

Lab9_PythonObjectOrientedProgramming

Uploaded by

ALI BOUANANI
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
4 views2 pages

Lab9_PythonObjectOrientedProgramming

Uploaded by

ALI BOUANANI
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 2

ENSA-Kenitra Module : Compétence Numériques

Lab 9: Python OOP:


Classes, Objects, Inheritance & Polymorphism

Exercise 1: Creating Basic Classes with Private Attributes and Methods

Define Basic Classes:

1. Create a class Person with the following private attributes:

 name (string) and age (integer).

2. Add:

 Getters and Setters for both name and age.


 A __str__ method that returns a formatted string with the person's name and age.
 A get_role_info() method that simply returns "Person: General information".

3. Create a class Employee that:

 Inherits from Person.


 Adds a private attribute salary (float).
 Adds getter and setter methods for salary.
 Overrides the __str__ method to include name, age, and salary in the formatted
string.
 Overrides the get_role_info() method to return "Employee: Salary details
available".

4. Instantiate a Person object and an Employee object, and then:

4.1. Print each object to see the output of the __str__ method.
4.2. Test the getters of the employee class by displaying the attributes’ values, and
then the setters for example: modify the employee object’s salary using the setter and
use the __str__ method to display the new salary.
4.2. Call get_role_info() on each object to see the different outputs and understand
how polymorphism works.

Pr. Mehdia AJANA Chapter7 – Python Object-Oriented Programming 1


ENSA-Kenitra Module : Compétence Numériques

Exercise 2: Hierarchical Inheritance with Getters, Setters, and Polymorphism

Hierarchical Inheritance with Method Overriding

1. Define a class Manager that inherits from Employee.

 Add a private attribute department (string).


 Add getter and setter methods for department.
 Override the __str__ method to include the department along with name, age, and
salary.
 Override the get_role_info() method to return "Manager: Department
management information".

2. Define another class Developer that also inherits from Employee.

 Add a private attribute programming_language (string).


 Add getter and setter methods for programming_language.
 Override the __str__ method to include the programming language along with name,
age, and salary.
 Override the get_role_info() method to return "Developer: Coding in
[programming_language]", where [programming_language] is replaced by the
value of the attribute.

Testing:

 Create a list containing instances of Person, Employee, Manager, and Developer.


 Set values for each object's attributes using the setters.
 Loop through the list:

 Print each object to see the output of __str__.


 Call get_role_info() on each object to demonstrate polymorphic behavior,
observing how each class provides unique role-specific information.

Pr. Mehdia AJANA Chapter7 – Python Object-Oriented Programming 2

You might also like