Saturday, March 2, 2024
HomeJavaQuiz your self: Java’s sealed sorts—true-or-false quiz

Quiz your self: Java’s sealed sorts—true-or-false quiz


Oracle Java, Java Certification, Java Guides, Java Learning, Java Tutorial and Materials

Check your information of legitimate sealed courses, subclasses, and the permits part.

Which assertion is true? Select one.

A. A sealed sort declaration should have a permits part.

B.  A sealed class should have a minimum of one direct subclass.

C. A sealed interface should have a minimum of one direct implementation.

D. A sealed sort may be an enum.

E. A sealed sort could be a file.

Reply. The purpose of a sealed sort hierarchy is to have source-level specs that make it very clear what sorts are permitted to be assignment-compatible with a given base sort.

Clearly, even should you don’t have a sealed sort hierarchy, you could find this data should you search all of the supply information utilized in your codebase. Nonetheless, that’s laborious at greatest, and it’s impractical when you have libraries that got here from different groups—or from third events.

As a broad overview, the sealed sort mechanism permits the supply for one sort to explicitly enumerate all its permitted subtypes. Every of these subtypes in flip is topic to restrictions which are typically supposed to maintain the complete tree clearly seen from the supply code.

One risk is {that a} subtype is remaining (both explicitly or implicitly, which is the case with a file sort). On this case, you’ve reached the top of the sort hierarchy on this department of the tree.

One other risk is to open up the hierarchy and permit arbitrary subtypes. This, to an extent, spoils the purpose of a sealed hierarchy, however it’s permitted as a result of sensible concerns would possibly warrant it. That is achieved by marking the sort as non-sealed.

A 3rd risk is that the subtype is an enum. An enum can by no means be named in an extends clause, which implies that the sealed father or mother sort should be an interface. Additionally, subtypes of an enum can exist solely when they’re declared as interior courses. As interior courses, they’re within the supply code of the enum itself, which makes it very clear what, if any, subtypes exist underneath this department of the sort tree.

The ultimate possibility is for the subtype itself to be marked as sealed. On this case, its kids may be expressly listed. Additionally on this case, the subtypes’ kids are topic to those guidelines. As soon as once more, it’s very clear from the supply which subtypes are attainable.

Whereas it’s regular for a sealed sort to enumerate its permitted kids with permits, there may be one state of affairs during which this may be omitted (notice that allows is obligatory in any other case). That state of affairs is when all of the youngster sorts are declared in the identical supply file (or compilation unit) because the father or mother sort. This consists of the potential for these sorts being interior or nested courses inside the father or mother sort.

This final risk tells you that possibility A is wrong.

The outline above omitted an essential element: The aim of a sealed sort hierarchy is to enumerate clearly the permitted youngster sorts. If you’d like zero kids of a given sort, that class ought to be marked as remaining (and, in fact, it should be a concrete class). A sealed sort should have a minimum of one subtype. This tells you that possibility B is right.

Possibility C is wrong. Any sealed sort should have some youngster sort, however it’s not crucial for an interface to have a direct implementation; it’s completely acceptable for it to have a subinterface. Observe that an interface can’t be marked as remaining, so a subinterface of a sealed interface should be both sealed or non-sealed to be legitimate.

Though an enum can’t be marked as remaining, a subtype of an enum should be an interior class. This already makes for tight management of the categories that may ever be assignment-compatible with it. After all, because of this an enum may be declared on a sealed sort hierarchy with out being labeled both sealed, non-sealed, or remaining. As famous earlier, the sealed father or mother sort of an enum should be an interface. It’s not helpful—and is, actually, not permitted—for an enum sort to be marked as sealed. Therefore, possibility D is wrong.

Document sorts are implicitly remaining; therefore, they can’t allow subtypes. You already know from the dialogue associated to possibility B that a minimum of one subclass is obligatory, so you’ll be able to deduce that possibility E can be incorrect.

Don’t overlook that it’s completely right for a file sort to seem in a sealed sort hierarchy; it merely should be a leaf node, not the basis of the hierarchy. It’s additionally price observing that since a file sort can not declare a father or mother class, all of the father or mother sorts of a file in a sealed hierarchy, as much as and together with the basis of that hierarchy, should be interfaces.

Conclusion. The right reply is possibility B.

Supply: oracle.com

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments