Language Upgrades · interview question
Consider the following code snippet of a sealed class hierarchy. Which class is incorrectly defined given the sealed class rules? sealed class Payment permits CreditCard, PayPal {} final class CreditCard extends Payment {} class Stripe extends Payment {} non-sealed class PayPal extends Payment {}?
Multiple choiceMediumchapter-12-curatedpremiumlanguage-upgradessealed-classes
sealed class Payment permits CreditCard, PayPal {}
final class CreditCard extends Payment {}
class Stripe extends Payment {}
non-sealed class PayPal extends Payment {}Answer and explanation
Correct answer
D. Stripe
Why
The class 'Stripe' is incorrectly defined because it extends the sealed class 'Payment' without being listed in the 'permits' clause. A sealed class restricts its subclasses to only those explicitly mentioned.
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?