Python OOP Assignment: Encapsulation, Inheritance & Polymorphism

Python OOP Assignment: Encapsulation, Inheritance & Polymorphism

Practice Assignment for Students

Object-Oriented Programming (OOP) is an important concept in Python. In this assignment, students will practice three major pillars of OOP:

  • Encapsulation
  • Inheritance
  • Polymorphism

The purpose of this assignment is to help students understand these concepts by creating simple, real-world examples in Python.

Language: Python
Level: Beginner / Intermediate
Topic: Object-Oriented Programming (OOP)


Pillar 1: Encapsulation

Encapsulation means keeping data protected inside a class and controlling how that data can be accessed or changed.

In Python, a private attribute can be created using double underscores (__). This helps prevent direct access to important data from outside the class.

1. The Secure Email Login System

Task:

The Secure Email Login System: Create a class named UserAccount with attributes username and a private attribute __password. Write a constructor to set these up. Create a method change_password(old_password, new_password) that checks if the old_password matches the private password before letting the user update it.

๐Ÿ’ก Understanding Tip

Think about a real email account. A user should not be able to simply change their password without first proving that they know the current password.

While completing this task, pay attention to:

  • How a class is created.
  • How the constructor initializes attributes.
  • How a private attribute is created.
  • How a method can safely update private data.
  • How an if condition can be used to verify the old password.

Practice Goal: Learn how to protect sensitive information inside a Python class.


2. The Car Fuel Tank Guard

Task:

The Car Fuel Tank Guard: Create a class named Car with a private attribute __fuel_level (initially set to 10 liters). Write a method refuel(liters) that adds fuel, but ensures the total fuel cannot exceed the tank capacity of 50 liters. If a user tries to refuel 100 liters, print an error message.

๐Ÿ’ก Understanding Tip

Imagine a car with a fuel tank that can hold a maximum of 50 liters. The program should not allow the fuel level to go above this limit.

Think about the following logic before writing your code:

Current Fuel + New Fuel โ‰ค Maximum Capacity

You will need to use a condition to check the fuel level before updating it.

Practice Goal: Understand how encapsulation can be used to protect data and enforce rules.


Pillar 2: Inheritance

Inheritance allows one class to use the properties and methods of another class.

It is useful when different classes share common characteristics. Instead of writing the same code again, a child class can inherit functionality from a parent class.


1. E-Commerce Products (Single Inheritance)

Task:

E-Commerce Products (Single Inheritance): Create a parent class called Product with attributes product_id and price. Create a child class called Clothing that inherits from Product and adds its own specific attributes like size and material. Create a Clothing object and print both the inherited price and the custom size.

๐Ÿ’ก Understanding Tip

Think about an online store.

A general Product may have information such as:

  • Product ID
  • Price

A Clothing product is still a product, but it may have additional information such as:

  • Size
  • Material

This is a simple example of single inheritance, where one child class inherits from one parent class.

Practice Goal: Learn how a child class can reuse attributes from its parent class while also having its own attributes.


2. School Management System (Multilevel Inheritance)

Task:

School Management System (Multilevel Inheritance): Create a grandparent class Person (attributes: name, age), a parent subclass Employee (adds employee_id), and a child subclass Teacher (adds subject). Write a constructor chain so that creating a Teacher object properly initializes their name, age, ID, and subject.

๐Ÿ’ก Understanding Tip

Here, you will create an inheritance chain:

Person โ†’ Employee โ†’ Teacher

Each class adds some new information:

  • Person โ†’ name, age
  • Employee โ†’ employee_id
  • Teacher โ†’ subject

The important part of this task is understanding how constructors work across multiple levels of inheritance.

Practice Goal: Understand how information can be passed through multiple classes using multilevel inheritance.


Pillar 3: Polymorphism

Polymorphism means “many forms.” In OOP, it allows the same method or function to behave differently depending on the object being used.

This is especially useful when different classes need to perform similar tasks in different ways.


1. E-Commerce Checkout Discounts (Method Overriding)

Task:

E-Commerce Checkout Discounts (Method Overriding): Create a parent class Discount with a method get_discount(price) that returns a default 5% discount. Create two child classes: VIPCustomer (overrides the method to give a 15% discount) and StudentDiscount (overrides the method to give a 10% discount). Write a function that takes any discount object, applies it to a price of $100, and displays the result.

๐Ÿ’ก Understanding Tip

This task demonstrates method overriding.

The parent class has a get_discount() method, but the child classes provide their own versions of that method.

For a price of $100:

  • Regular discount โ†’ 5%
  • VIP customer โ†’ 15%
  • Student โ†’ 10%

The method name remains the same, but its behavior changes depending on which object is passed to the function.

Practice Goal: Understand how different objects can use the same method name but produce different results.


2. Smart Assistant Broadcaster (Compile-time / Optional Parameters)

Task:

Smart Assistant Broadcaster (Compile-time / Optional Parameters): Create a class SmartAssistant with a single method send_alert(message, medium=”Speaker”, volume=50). Show how this single method can behave dynamically depending on whether the user calls it with just a message, changes the medium to “Phone”, or adjusts the volume parameter.

๐Ÿ’ก Understanding Tip

This task focuses on optional parameters in Python.

The method already has default values:

  • medium="Speaker"
  • volume=50

This means the user does not always need to provide every argument.

Try calling the method in different ways and observe how the output changes. For example, think about what should happen when:

  • Only the message is provided.
  • The medium is changed to "Phone".
  • The volume is changed.

Practice Goal: Understand how default and optional parameters can make one method flexible and reusable.


๐ŸŽฏ Assignment Practice Goals

After completing this assignment, students should be able to:

  • Create classes and objects in Python.
  • Use constructors to initialize object data.
  • Understand private attributes and encapsulation.
  • Create parent and child classes.
  • Use single and multilevel inheritance.
  • Override methods in child classes.
  • Understand polymorphism through practical examples.
  • Work with default and optional parameters.
  • Connect OOP concepts with real-world scenarios.

๐Ÿ“ Student Practice Tips

Before starting the assignment, make sure you understand the basic concepts of:

  • Classes and objects
  • __init__() constructor
  • Attributes and methods
  • if conditions
  • Inheritance
  • super()
  • Method overriding
  • Function parameters

Challenge Yourself ๐Ÿš€

Don’t just copy a solution from somewhere. First try to design the class structure yourself.

For every question, ask yourself:

“What should the class contain?”
“What information should the object store?”
“What should the method do?”
“What condition or rule needs to be checked?”

Once you can answer these questions, writing the Python code becomes much easier.


โœ… Final Note

This assignment is designed for practice, so students are encouraged to experiment with the code.

Try creating your own objects, changing values, testing different inputs, and observing the output. Making small mistakes while practicing is a normal and important part of learning programming.

Happy Coding! ๐Ÿ๐Ÿ’ป