Object-Oriented Programs

Python programs that demonstrate classes, objects, inheritance, polymorphism, encapsulation, and common OOP patterns.

25 program solutions

Back to Topics

Program Solutions

1

Create Class & Object

Define a simple class with attributes and methods, then create and use an object of that class.

Beginner
View Solution
2

Instance vs Class Variables

Demonstrate the difference between instance variables and class variables.

Beginner
View Solution
3

Constructors in Python Classes

Use the __init__ constructor to initialize new objects with default and custom values.

Beginner
View Solution
4

Single Inheritance

Show how a child class can inherit attributes and methods from a single parent class.

Beginner
View Solution
5

Multiple Inheritance

Demonstrate a class inheriting from more than one base class.

Intermediate
View Solution
6

Multilevel Inheritance

Illustrate inheritance across multiple levels of a class hierarchy.

Beginner
View Solution
7

Polymorphism with Methods

Use polymorphism by defining the same method name in different classes.

Beginner
View Solution
8

Method Overloading Simulation

Simulate method overloading using default arguments and *args.

Beginner
View Solution
9

Encapsulation with Getters/Setters

Use properties to encapsulate attribute access with getter and setter logic.

Intermediate
View Solution
10

Abstraction with ABC

Use the abc module to define abstract base classes and abstract methods.

Intermediate
View Solution
11

Operator Overloading

Overload the + operator for a custom class using __add__.

Intermediate
View Solution
12

Custom Iterator

Implement a class that can be iterated over using __iter__ and __next__.

Intermediate
View Solution
13

Class Methods

Use @classmethod to create alternative constructors.

Intermediate
View Solution
14

Static Methods

Use @staticmethod for utility methods that logically belong to the class but do not use self or cls.

Beginner
View Solution
15

Composition Example

Use composition by placing one object inside another to build complex behavior.

Intermediate
View Solution
16

Aggregation Example

Demonstrate aggregation where an object is passed in and shared rather than owned.

Intermediate
View Solution
17

Private Variables

Use name-mangling with double underscores to indicate private attributes.

Intermediate
View Solution
18

Magic Methods Overview

Show common magic methods like __str__ and __len__.

Intermediate
View Solution
19

Object Cloning

Clone objects using the copy module (shallow and deep copy).

Intermediate
View Solution
20

Custom Exceptions

Define and raise custom exception classes in an OOP style.

Intermediate
View Solution
21

Class Decorator

Use a class as a decorator to wrap functions with additional behavior.

Intermediate
View Solution
22

Prototype Pattern

Implement a simple Prototype pattern by cloning existing objects.

Intermediate
View Solution
23

Singleton Pattern

Demonstrate a simple Singleton pattern implementation in Python.

Intermediate
View Solution
24

MRO (Method Resolution Order) Demo

Show how Python resolves methods in a multiple inheritance hierarchy using MRO.

Intermediate
View Solution
25

Polymorphism (Duck Typing)

Use duck typing to write functions that work with any object having the required method.

Intermediate
View Solution