Monday, June 23, 2025
HomeJavaQuiz your self: Java’s sealed-type mechanism and the module-info.java file

Quiz your self: Java’s sealed-type mechanism and the module-info.java file


Think about you’re engaged on a brand new unit testing framework for Java courses and begin with the next folder hierarchy:

src

 └───com

    └───acme

        └───take a look at

              └───Check.java

There’s a single base Java class contained in the challenge.

Core Java, Oracle Java Career, Oracle Java Skills, Oracle Java Jobs, Oracle Java Preparation, Oracle Java Tutorial and Materials, Oracle Java Guides, Oracle Java Certification

bundle com.acme.take a look at;

// import assertion(s) right here if any

summary sealed public class Check permits StaticTest, DynamicTest {

    summary public void take a look at();

}

Which statements appropriately describe the circumstances when the code compiles efficiently? Select two.

A. For those who put the StaticTest and DynamicTest courses within the com.acme.take a look at bundle, you don’t want to supply module-info.java.

B. For those who put the StaticTest and DynamicTest courses within the com.acme.take a look at.util bundle, you don’t want to supply module-info.java.

C. For those who present module-info.java, you should put the StaticTest and DynamicTest courses in the identical module with the Check class.

D. For those who present module-info.java, you should put the StaticTest and DynamicTest courses within the com.acme.take a look at bundle.

E. If all modules have a correct module-info.java file, you could put the StaticTest and DynamicTest courses in numerous modules.

Reply. This query investigates some key restrictions of the sealed-type mechanism.

The sealed-type mechanism permits supply code to make it very clear that solely a particular set of subtypes are assignment-compatible with the sealed base sort. In fact, it’s at all times been doable to restrict the variety of sorts which can be assignment-compatible with a given sort just by not coding extra courses than you need. However in that older method, the design intent isn’t specific within the code, and if others who’re unfamiliar along with your intentions work on the code, they may break this intention and create bugs by mistake.

One other advantage of sealed sorts is that if a naively made change provides a brand new assignment-compatible sort, it causes a syntax error, alerting the programmer that the change needs to be mentioned with others who’re totally conversant in the design.

A 3rd profit, which is predicted to be realized absolutely when the ultimate model of sample matching for swap is launched, is the power to alert programmers to different areas of code that should be up to date if new assignment-compatible subtypes are added by the correct syntactic mechanism. (Particularly, the swap assertion will be capable to concern an error if the brand new sort is omitted from the set of patterns talked about in case clauses.)

The courses specified by permits should be positioned close to the superclass: both in the identical module (if the superclass is in a named module) or in the identical bundle (if the superclass is within the unnamed module).

Discover this enumerates two prospects.

◉ All parts of the sealed-type hierarchy should be in the identical module.

◉ If the code that’s compiled and run isn’t in an specific module (that’s, the code on the classpath is within the unnamed module, which isn’t specific), the weather of the hierarchy should be in the identical bundle.

It’s price noting that the Java compiler determines whether or not code is meant to be used within the module system by figuring out the presence or absence of a module-info.java file. That is considerably approximate, since code might be compiled right into a module however then be executed from the unnamed module if the JAR file containing the code is positioned on the classpath throughout execution.

From the principles listed in JEP 409, you already know that it’s necessary that each one parts of the sealed hierarchy be in the identical bundle if the code isn’t working in a named module. This makes possibility An accurate.

You additionally know that it’s permissible to put members of the sealed hierarchy in numerous packages provided that the code is working in a named module. Because the compiler approximates this dedication primarily based on the presence or absence of a module-info.java file, possibility B is inaccurate.

Possibility C can be right as a result of, as talked about above, permitted sorts should be as shut as doable to sealed sorts, and essentially the most permissive interpretation is that each one the categories should be in the identical module. One issue right here is that modules are generally developed and compiled independently, which implies it’d be a lot tougher to maintain observe of the entire hierarchy if this rule didn’t exist.

Possibility D is inaccurate. It’s not necessary to place all of the permitted sorts into the identical bundle if you construct a sealed-type hierarchy with a module-info.java file, which the compiler interprets as constructing a module (even when the code may not be run in that manner).

Possibility E can be incorrect, as a result of you’ll be able to by no means unfold a sealed sort and its permitted sorts throughout completely different modules.

Conclusion. The proper solutions are choices A and C.

Supply: oracle.com

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments