Skip to content
Sharp

Language Upgrades · interview question

Given the following code, what would be the correct way to allow 'GiftCard' to extend 'Payment'? sealed class Payment permits CreditCard, PayPal {} final class CreditCard extends Payment {} non-sealed class PayPal extends Payment {}?

Multiple choiceMediumchapter-12-curatedpremiumlanguage-upgradessealed-classes
java
sealed class Payment permits CreditCard, PayPal, GiftCard {}
Choose one answer
Answer and explanation

Correct answer

A. Add 'GiftCard' to the permits clause of 'Payment'.

Why

To allow 'GiftCard' to extend 'Payment', you must add 'GiftCard' to the 'permits' clause of the 'Payment' class. This explicitly allows 'GiftCard' to be part of the sealed class hierarchy, maintaining control over subclassing.

Test yourself on Language Upgrades

A free test with instant, explained results. No account needed.

Start free test
All Language Upgrades questions →