← Back to Dashboard

Object Oriented Programming: Medium Exercises MEDIUM

"""
Topic 07: Object Oriented Programming (OOP) - MEDIUM Exercises (10)
"""

# Exercise 1: Single Inheritance - Create a class 'Animal' with a method 'speak'. 
# Create a child class 'Cat' that overrides 'speak' to say "Meow".
# Write your code below:


# Exercise 2: Multilevel Inheritance - Person -> Employee -> Manager. 
# Each class should add a unique attribute or method.
# Write your code below:


# Exercise 3: Polymorphism - Create a list containing objects of classes 'Dog', 'Cat', and 'Cow'. 
# Call the 'speak' method on each in a loop.
# Write your code below:


# Exercise 4: Encapsulation - Create a class 'BankAccount' with a private attribute '__balance'. 
# Add methods 'deposit' and 'withdraw' with validation (e.g., can't withdraw more than balance).
# Write your code below:


# Exercise 5: Method Overloading (Simulation) - Python doesn't support traditional method overloading. 
# Write a method 'area' in class 'Shape' that behaves differently based on the number of arguments (1 for Circle, 2 for Rectangle).
# Write your code below:


# Exercise 6: Property Decorators - Use @property to create a getter and setter for a 'salary' attribute 
# in an 'Employee' class. Ensure the salary cannot be negative.
# Write your code below:


# Exercise 7: Magic Methods - Implement the __str__ method in a 'Point' class (x, y) 
# to print in the format "(x, y)".
# Write your code below:


# Exercise 8: Addition of Objects - Use the __add__ magic method to allow adding two 'Point' objects together.
# Write your code below:


# Exercise 9: Class Methods vs Static Methods - Create a class with one of each. 
# Explain the difference in a comment.
# Write your code below:


# Exercise 10: Create a class 'Library' that stores a list of 'Book' objects. 
# Add a method to search for a book by title.
# Write your code below: