Bird
0
0

Find the bug in this code snippet implementing Payment Strategy Pattern:

medium📝 Analysis Q7 of 15
LLD - Design — Online Shopping Cart
Find the bug in this code snippet implementing Payment Strategy Pattern:
interface PaymentStrategy {
  void pay(double amount);
}

class CashPayment implements PaymentStrategy {
  public void pay(int amount) {
    System.out.println("Paid " + amount + " in cash");
  }
}
AMethod signature in CashPayment does not match interface
BInterface method should return int
CCashPayment class should be abstract
DInterface should have default implementation
Step-by-Step Solution
Solution:
  1. Step 1: Compare method signatures

    Interface method pay takes double, but CashPayment implements pay with int parameter, causing signature mismatch.
  2. Step 2: Check other options

    Interface method return type void is correct, no need for abstract class or default implementation here.
  3. Final Answer:

    Method signature in CashPayment does not match interface -> Option A
  4. Quick Check:

    Method signature mismatch causes compile error [OK]
Quick Trick: Method signatures must exactly match interface [OK]
Common Mistakes:
  • Changing parameter types in implementation
  • Adding return type mismatch
  • Assuming abstract class needed

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes