Bird
0
0

Which of the following is the correct way to implement a Null Object in a class hierarchy?

easy🧠 Conceptual Q12 of 15
LLD - Behavioral Design Patterns — Part 2
Which of the following is the correct way to implement a Null Object in a class hierarchy?
ACreate a subclass that overrides methods with empty implementations
BUse a global variable set to null
CThrow exceptions in the Null Object methods
DReturn null from all methods in the Null Object
Step-by-Step Solution
Solution:
  1. Step 1: Identify how Null Object should behave

    It should implement the same interface but provide harmless (empty) behavior.
  2. Step 2: Choose the correct implementation approach

    Creating a subclass that overrides methods with empty implementations fits the pattern.
  3. Final Answer:

    Create a subclass that overrides methods with empty implementations -> Option A
  4. Quick Check:

    Null Object subclass overrides methods safely [OK]
Quick Trick: Null Object overrides methods with empty bodies [OK]
Common Mistakes:
MISTAKES
  • Using null variables instead of objects
  • Throwing exceptions defeats Null Object purpose
  • Returning null causes errors again

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes