0
0
LLDsystem_design~10 mins

Open/Closed Principle in LLD - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a class that follows the Open/Closed Principle by allowing extension without modification.

LLD
class Shape {
    public void draw() {
        // base draw method
    }
}

class Circle extends Shape {
    @Override
    public void [1]() {
        System.out.println("Drawing Circle");
    }
}
Drag options to blanks, or click blank then click option'
Arender
Bpaint
Cdraw
Ddisplay
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name that does not override the base class method.
Modifying the base class method directly instead of overriding.
2fill in blank
medium

Complete the code to add a new shape class without modifying existing classes, adhering to the Open/Closed Principle.

LLD
interface Shape {
    void draw();
}

class Rectangle implements Shape {
    public void draw() {
        System.out.println("Drawing Rectangle");
    }
}

class [1] implements Shape {
    public void draw() {
        System.out.println("Drawing Triangle");
    }
}
Drag options to blanks, or click blank then click option'
ACircle
BTriangle
CSquare
DEllipse
Attempts:
3 left
💡 Hint
Common Mistakes
Modifying existing classes to add new shape logic.
Not implementing the Shape interface in the new class.
3fill in blank
hard

Fix the error in the code to ensure the Open/Closed Principle is not violated when adding new payment methods.

LLD
abstract class Payment {
    abstract void pay();
}

class CreditCardPayment extends Payment {
    void pay() {
        System.out.println("Paying with Credit Card");
    }
}

class PaymentProcessor {
    public void processPayment(Payment payment) {
        payment.[1]();
    }
}
Drag options to blanks, or click blank then click option'
Apay
Bprocess
Cexecute
DmakePayment
Attempts:
3 left
💡 Hint
Common Mistakes
Calling a method name not declared in the Payment class.
Adding conditional logic in processPayment to handle new payment types.
4fill in blank
hard

Fill both blanks to implement a notification system that follows the Open/Closed Principle by supporting multiple notification types.

LLD
interface Notification {
    void send();
}

class EmailNotification implements Notification {
    public void [1]() {
        System.out.println("Sending Email");
    }
}

class NotificationService {
    public void notifyUser(Notification notification) {
        notification.[2]();
    }
}
Drag options to blanks, or click blank then click option'
Asend
Bnotify
Calert
Dmessage
Attempts:
3 left
💡 Hint
Common Mistakes
Using different method names in interface and implementation.
Adding conditional logic in NotificationService for each notification type.
5fill in blank
hard

Fill all three blanks to implement a logging system that adheres to the Open/Closed Principle by allowing new loggers without modifying existing code.

LLD
interface Logger {
    void [1](String message);
}

class ConsoleLogger implements Logger {
    public void [2](String message) {
        System.out.println("Console: " + message);
    }
}

class LoggerService {
    public void logMessage(Logger logger, String message) {
        logger.[3](message);
    }
}
Drag options to blanks, or click blank then click option'
Alog
BlogMessage
Dwrite
Attempts:
3 left
💡 Hint
Common Mistakes
Using different method names in interface and implementation.
Adding conditional logic in LoggerService for different logger types.