Practical Example: Classes and Objects

Defining a Class for Geometric Shapes

Problem Statement: You are writing software to calculate the area and perimeter of basic geometric shapes. Define a Rectangle class using an Object-Oriented approach and instantiate an object from it.

Step-by-Step Solution

0 of 3 Steps Completed
1

Practical Example: Advanced Inheritance (Interfaces & Abstract Classes)

Designing an Abstract 'Vehicle' Base Class

Problem Statement: Design a system that handles various vehicles (cars, motorcycles, trucks). Every vehicle must have a start_engine() method, but the implementation differs. Use an Abstract Base Class (ABC) to enforce this requirement.

Step-by-Step Solution

0 of 3 Steps Completed
1

Practical Example: SOLID Principles

Applying the Single Responsibility Principle (SRP)

Problem Statement: The following class violates the Single Responsibility Principle by handling both report generation and file saving. Refactor it to adhere to SRP.
class ReportManager:
    def generate_report(self, data):
        return f"Report Data: {data}"

    def save_to_file(self, content, filename):
        with open(filename, 'w') as f:
            f.write(content)

Step-by-Step Solution

0 of 3 Steps Completed
1

Practical Example: Basic Inheritance

Implementing Inheritance in Python

Problem Statement: Model a Vehicle base class and a Car derived class in Python, demonstrating how the child class inherits attributes and methods from the parent.

Step-by-Step Solution

0 of 4 Steps Completed
1

Applying the Open/Closed Principle (OCP)

Problem Statement: A discount calculation module currently checks the customer type using a rigid if-else block. If a new customer type is added, the core function must be modified, violating OCP. Refactor it so the module is "open for extension, but closed for modification".

Step-by-Step Solution

0 of 3 Steps Completed
1