This repository contains solutions for 20 Python Object-Oriented Programming (OOP) tasks, covering concepts like self, cls, public/private/protected variables, inheritance, abstraction, decorators, static/class methods, MRO, custom exceptions, and more.
- 1. Using self
- 2. Using cls
- 3. Public Variables and Methods
- 4. Class Variables and Class Methods
- 5. Static Variables and Static Methods
- 6. Constructors and Destructors
- 7. Access Modifiers: Public, Private, and Protected
- 8. The super() Function
- 9. Abstract Classes and Methods
- 10. Instance Methods
- 11. Class Methods
- 12. Static Methods
- 13. Composition
- 14. Aggregation
- 15. Method Resolution Order (MRO) and Diamond Inheritance
- 16. Function Decorators
- 17. Class Decorators
- 18. Property Decorators
- 19. callable() and call()
- 20. Creating a Custom Exception
Created a Student class using self to initialize attributes name and marks through a constructor. A method display() prints the student details.
Created a Counter class to track how many objects are created using a class variable and a @classmethod.
Built a Car class with a public variable brand and a public method start() accessible outside the class.
Developed a Bank class with a class variable bank_name and a class method change_bank_name() to change it.
Created MathUtils with a static method add(a, b) that performs addition without using class or instance variables.
Designed a Logger class that prints messages when an object is created and destroyed.
Made an Employee class with public (name), protected (_salary), and private (__ssn) attributes.
Implemented inheritance where Teacher class inherits from Person and uses super() to call the parent constructor.
Utilized the abc module to define an abstract class Shape with an abstract method area(), and implemented it in Rectangle.
Built a Dog class with instance variables and an instance method bark().
Created a Book class to track the number of books using a class method increment_book_count().
Developed a TemperatureConverter class with a static method celsius_to_fahrenheit().
Used composition by embedding an Engine object inside a CarWithEngine class.
Showed aggregation through Department and EmployeeAgg classes where the Employee exists independently.
Demonstrated MRO with classes A, B, C, and D, where D inherits from both B and C.
Built a decorator log_function_call to log a message before executing a function.
Created a class decorator add_greeting to dynamically add a greet() method to a class.
Designed a Product class with a private attribute _price using @property, @setter, and @deleter.
Made a Multiplier class implementing __call__() to allow objects to behave like functions.
Defined a custom exception InvalidAgeError and raised it from a check_age(age) function if age < 18.
- Make sure you have Python 3.x installed.
- Save the code into a file named `assignment.py