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

Week 2 - Deeper OOP Fundamentals (Encapsulation and Inheritance) - Lab Without Answer (20250131120633)

The document outlines a laboratory activity focused on implementing encapsulation and inheritance in Python through a School Management System and a Zoo Animal Management System. It includes objectives, instructions for creating base and subclass structures, and testing methods for managing attributes securely. Additionally, it poses assessment questions regarding access modifiers and method overriding.

Uploaded by

mythesports2005
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)
4 views8 pages

Week 2 - Deeper OOP Fundamentals (Encapsulation and Inheritance) - Lab Without Answer (20250131120633)

The document outlines a laboratory activity focused on implementing encapsulation and inheritance in Python through a School Management System and a Zoo Animal Management System. It includes objectives, instructions for creating base and subclass structures, and testing methods for managing attributes securely. Additionally, it poses assessment questions regarding access modifiers and method overriding.

Uploaded by

mythesports2005
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/ 8

Name:

Section:
Date:

Laboratory Activity: Encapsulation and Inheritance

Title: "School Management System: Leveraging Encapsulation and Inheritance:

Objectives

1. Implement encapsulation using public, protected, and private access modifiers.


2. Use inheritance to create a hierarchy of classes.
3. Demonstrate the principles of object-oriented programming in Python.

Instructions

1. Base Class: Create a base class called Person:


o Attributes:
▪ name (string, public): The person's name.
▪ _age (integer, protected): The person's age.
▪ __address (string, private): The person's address.
o Methods:
▪ A constructor to initialize the attributes.
▪ A public method get_address() to access the private address.
▪ A public method set_address(new_address) to update the address.
▪ A protected method _get_age() to access the protected age.
▪ A public method display_info() to display the person's details (name
and age).
2. Subclass: Student:
o Attributes:
▪ student_id (string, public): The student’s ID.
▪ _course (string, protected): The course the student is enrolled in.
o Methods:
▪ A public method get_course() to access the protected course.
▪ Override display_info() to include the student’s ID and course.
3. Subclass: Teacher:
o Attributes:
▪ subject (string, public): The subject the teacher teaches.
▪ _salary (float, protected): The teacher’s salary.
o Methods:
▪ A public method get_salary() to access the protected salary.
▪ Override display_info() to include the teacher’s subject and salary.

4. Test Cases:
o Create a Student object and a Teacher object.
o Set and get their private and protected attributes using appropriate methods.
o Call display_info() for both objects to display all details.

Additional Task

• Add a method update_age() in the Person class to increase or decrease the person's
age. Test this method in both subclasses.

Code, Expected Output, and UML Diagram:


Activity: Zoo Animal Management System

Activity Title: "Managing Animals in a Zoo Using Inheritance and Access Modifiers"

Objective:

1. Use inheritance to model a hierarchy of animals in a zoo.


2. Apply public, protected, and private attributes for secure data management.
3. Override methods to provide customized behavior for different animal types.

Instructions:

1. Define a base class Animal:


o Attributes:
▪ name (string, public): The name of the animal.
▪ _species (string, protected): The species of the animal.
▪ __diet (string, private): The diet of the animal (e.g., "Herbivore",
"Carnivore", "Omnivore").
o Methods:
▪ A constructor to initialize the attributes.
▪ A public method get_diet() to return the private diet.
▪ A public method set_diet(new_diet) to update the diet. Validate the
input to ensure it’s one of the allowed values: "Herbivore", "Carnivore",
"Omnivore".
▪ A protected method _get_species() to return the protected _species.
▪ A public method display_info() to display the animal’s details (name,
species, and diet).
2. Create the following subclasses:
o Bird:
▪ Add an attribute wing_span (float, public): The wingspan of the bird in
meters.
▪ Override the display_info() method to include the wingspan.
o Mammal:
▪ Add an attribute is_nocturnal (boolean, public): Whether the mammal is
nocturnal.
▪ Override the display_info() method to include the nocturnal status.
3. Test the classes:
o Create a Bird object and a Mammal object.
o Use set_diet() and get_diet() to interact with the private diet attribute.
o Call the display_info() method for both objects.
4. Additional Task:
o Add a Reptile subclass with a public attribute temperature_preference
(string) and override display_info() to include this attribute.
o Add a Reptile subclass with attributes temperature_preference (e.g., "Cold",
"Warm").
o Add a method feed_animal(food) in the Animal class that validates the food
based on the animal’s diet.

Code, Expected Output, and UML Diagram:


Assessment Questions

1. What is the purpose of using access modifiers (public, protected, and private) in a class?
2. Why do we use super() in the subclasses?
3. What are the advantages of overriding methods in a subclass?

You might also like