Skip to content
Sharp

Language Upgrades · interview question

Consider the following code snippet. What will be the result of compiling this code?

Multiple choiceHardchapter-12-curatedpremiumlanguage-upgradessealed-classes
java
sealed class Order permits OnlineOrder, StoreOrder {}
class ShopOrder extends Order {}
Choose one answer
Answer and explanation

Correct answer

B. Compilation error: Non-permitted subclass 'ShopOrder'.

Why

The class 'ShopOrder' is not listed in the 'permits' clause of the sealed class 'Order', leading to a compilation error as it violates the sealed class hierarchy.

Test yourself on Language Upgrades

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

Start free test
All Language Upgrades questions →