Language Upgrades · interview question
Which statement about the 'sealed' keyword in Java is correct?
Multiple choiceMediumchapter-12-curatedpremiumlanguage-upgradespattern-matching
Answer and explanation
Correct answer
B. It defines a class or interface that can only be subclassed by a specific set of classes.
Why
The 'sealed' keyword is used to restrict which classes can extend or implement a class or interface, allowing only specific classes to do so.
Test yourself on Language Upgrades
A free test with instant, explained results. No account needed.
More Language Upgrades questions
- 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?
- Consider the following code snippet. What will be the result of compiling this code?