Bird
0
0

Why does ResponseEntityExceptionHandler use protected methods for handling exceptions instead of public methods?

hard📝 Conceptual Q10 of 15
Spring Boot - Exception Handling
Why does ResponseEntityExceptionHandler use protected methods for handling exceptions instead of public methods?
ATo allow subclasses to override handlers while preventing external access.
BBecause public methods cannot return ResponseEntity.
CTo restrict usage only to classes in the same package.
DTo enforce that only Spring framework can call these methods.
Step-by-Step Solution
Solution:
  1. Step 1: Understand method visibility in inheritance

    Protected methods allow subclasses to override them but hide them from outside classes.
  2. Step 2: Evaluate other options

    Public methods can return ResponseEntity; package-private restricts too much; Spring calls via reflection, not visibility enforcement.
  3. Final Answer:

    To allow subclasses to override handlers while preventing external access. -> Option A
  4. Quick Check:

    Protected = subclass override, no external access [OK]
Quick Trick: Protected methods enable subclass overrides safely [OK]
Common Mistakes:
  • Thinking public is required for Spring calls
  • Confusing protected with package-private
  • Assuming visibility affects return types

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes