Object-Oriented Programs
Python programs that demonstrate classes, objects, inheritance, polymorphism, encapsulation, and common OOP patterns.
25 program solutions
Program Solutions
Create Class & Object
Define a simple class with attributes and methods, then create and use an object of that class.
Instance vs Class Variables
Demonstrate the difference between instance variables and class variables.
Constructors in Python Classes
Use the __init__ constructor to initialize new objects with default and custom values.
Single Inheritance
Show how a child class can inherit attributes and methods from a single parent class.
Multiple Inheritance
Demonstrate a class inheriting from more than one base class.
Multilevel Inheritance
Illustrate inheritance across multiple levels of a class hierarchy.
Polymorphism with Methods
Use polymorphism by defining the same method name in different classes.
Method Overloading Simulation
Simulate method overloading using default arguments and *args.
Encapsulation with Getters/Setters
Use properties to encapsulate attribute access with getter and setter logic.
Abstraction with ABC
Use the abc module to define abstract base classes and abstract methods.
Operator Overloading
Overload the + operator for a custom class using __add__.
Custom Iterator
Implement a class that can be iterated over using __iter__ and __next__.
Class Methods
Use @classmethod to create alternative constructors.
Static Methods
Use @staticmethod for utility methods that logically belong to the class but do not use self or cls.
Composition Example
Use composition by placing one object inside another to build complex behavior.
Aggregation Example
Demonstrate aggregation where an object is passed in and shared rather than owned.
Private Variables
Use name-mangling with double underscores to indicate private attributes.
Magic Methods Overview
Show common magic methods like __str__ and __len__.
Object Cloning
Clone objects using the copy module (shallow and deep copy).
Custom Exceptions
Define and raise custom exception classes in an OOP style.
Class Decorator
Use a class as a decorator to wrap functions with additional behavior.
Prototype Pattern
Implement a simple Prototype pattern by cloning existing objects.
Singleton Pattern
Demonstrate a simple Singleton pattern implementation in Python.
MRO (Method Resolution Order) Demo
Show how Python resolves methods in a multiple inheritance hierarchy using MRO.
Polymorphism (Duck Typing)
Use duck typing to write functions that work with any object having the required method.