Language Upgrades · interview question
Why is a sealed hierarchy a good fit for PaymentResult with only Success, Failure, and Pending states?
Multiple choiceMediumchapter-12-curatedpremiumlanguage-upgradessealed-classes
Answer and explanation
Correct answer
A. Because the business owns a closed set of valid outcomes and the compiler can enforce exhaustive handling
Why
Sealed hierarchies work best when the valid variants are truly finite and controlled by the business. That lets the compiler check exhaustiveness and helps prevent accidental unsupported states.
Test yourself on Language Upgrades
A free test with instant, explained results. No account needed.
More Language Upgrades questions
- Which of the following is NOT a valid modifier for a class extending a sealed class?
- How does pattern matching for 'instanceof' improve maintainability in Java?
- What must be true for a class to extend a sealed class and be marked as 'non-sealed'?
- Which Java keyword would you use to specify the classes that can extend a sealed class?
- Given a sealed interface, which of the following is NOT a correct way to implement it?
- Why might an architect choose sealed classes for an e-commerce domain model?