Monday, November 28, 2022
HomeJavaQuiz your self: Courses, modules, and sealed varieties

Quiz your self: Courses, modules, and sealed varieties


Oracle Java, Oracle Java Prep, Oracle Java Certification, Oracle Java Learning, Java Prep, Java Preparation, Java Guides, Java Career, Java Skills, Java Jobs


Know what Java Language Specification 8.1.6 says about sealed-type hierarchies.

Given three courses that efficiently compile

package deal com.foo;

public sealed class Tremendous permits com.bar.SubBar, com.baz.SubBaz {

}

package deal com.bar;

import com.foo.Tremendous;

public closing class SubBar extends Tremendous {

}

package deal com.baz;

import com.foo.Tremendous;

public closing class SubBaz extends Tremendous {

}

Which assertion is right? Select one.

A. The courses should belong to the unnamed module.

B. The courses should belong to the identical unnamed or named module.

C. The courses should belong to a single named module.

D. The courses might belong to completely different named modules.

Reply. Sealed courses, developed beneath JEP 409, had been initially launched as a preview in Java 15 and finalized in Java 17. The code proven is a syntactically legitimate declaration of three courses in a sealed-class hierarchy.
Discover that the kids consult with the guardian utilizing the extends clause, and the guardian refers back to the kids utilizing the permits clause. That is really a round reference and is indicative of the very tight coupling between the varieties of a sealed-type hierarchy. Certainly, the Java Language Specification 8.1.6 affords the next design steering on the problem of sealed courses:

… a sealed class hierarchy ought to all the time be declared inside a single upkeep area, the place the identical developer or group of builders is answerable for sustaining the hierarchy.

In reality, the JLS imposes syntactic guidelines on the kinds in a sealed-type hierarchy, additionally in part 8.1.6. It notes the next:

If a sealed class C is related to a named module, then each class specified within the permits clause of C’s declaration should be related to the identical module as C, or a compile-time error happens.

If a sealed class C is related to an unnamed module, then each class specified within the permits clause of C’s declaration should belong to the identical package deal as C, or a compile-time error happens.

The primary of these two paragraphs says that the sealed class (Tremendous) and its permitted subclasses (SubBar, SubBaz) should belong to the identical module. From this you may decide that choice D is inaccurate and that choice C is legitimate.

The second paragraph says that you just can’t outline a sealed-type hierarchy in an unnamed module if any of the kinds are in several packages. This tells you that choices A and B are incorrect.

Since choice C is now the one remaining legitimate choice, you may lastly make sure that it’s the right reply.

Facet word: The round references talked about earlier (the place the kid varieties consult with the guardian kind of their extends clauses, and the guardian kind refers back to the little one varieties within the permits clause) mix with the module system rule that prohibits cyclic dependencies amongst modules. This prevents the varieties of the sealed-type hierarchy from being unfold throughout modules.

Conclusion. The proper reply is choice C.

Supply: oracle.com

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments