Saturday, June 22, 2024
HomeJavaQuiz your self: Controlling the variety of cases of a category

Quiz your self: Controlling the variety of cases of a category

Which is one of the best strategy: singletons, sealed courses, summary courses, last courses, or enums?

Think about that your design requires precisely 4 cases of a specific class.

Quiz Yourself, Oracle Java, Java Tutorial and Materials, Java Certification, Java Career, Java Skills, Java Jobs, Java Prep, Java Preparation

Which strategy finest supplies for this want? Select one.

A. Singletons

B. Sealed courses

C. Summary courses

D. Remaining courses

E. Enums

Reply. Possibility A is wrong as a result of singleton isn’t a Java key phrase, neither is it an idea outlined within the Java Language Specification. Singleton is definitely the title of a design sample and as a design sample, it may be applied in a number of other ways.

From the attitude of this query, a very powerful attribute of a singleton is that it limits the variety of cases of its sort to 1 per JVM. The quiz query merely requires that the variety of cases be mounted; it doesn’t require that quantity to be one, and as such, an strategy is important that permits the occasion rely to be larger than one. Consequently, choice A is wrong.

Possibility B can also be incorrect. Sealed courses had been added in Java 17, and the function’s aim is to repair a set of varieties that exist as specializations of a specific sort. For instance, you can specify {that a} class RoadTransport is permitted to have solely subtypes Automotive, Truck, Bus, Motorbike, and Bicycle. Be aware that the sealed courses function doesn’t restrict the variety of cases of those courses that may exist.

As well as, choice C is wrong. Summary courses are a mechanism to seize the frequent performance that’s shared by a number of varieties in a single class, and so they require builders so as to add particular extra performance to subclasses. As a aspect be aware, an summary class will be sealed, however as earlier than, this doesn’t have an effect on the variety of cases you may create.

Possibility D can also be incorrect. The ultimate modifier, when utilized to a category, means you can not subclass that class. Nevertheless, the modifier doesn’t indicate any management on the variety of cases.

Possibility E is appropriate, as a result of an enum sort supplies a transparent, language-supported resolution to regulate the variety of cases of a category. There are some limitations on enum varieties; notably, the father or mother class of an enum is all the time java.lang.Enum, although an enum can implement interfaces of your selecting.

A second limitation is that an enum is all the time a last class and can’t be subclassed. That is necessary, since cases of a subclass are additionally cases of their mother and father, and if subclassing had been potential, these subclasses would break the contract of a hard and fast variety of cases.

It’s value mentioning {that a} programmer can tightly management the development of any class, and with it the variety of cases of such a category, by marking all of the constructors of that class as non-public and offering entry to cases by means of a manufacturing facility methodology. Actually, this concept is important to the implementation of each Java’s enum sort and the Singleton design sample. Nevertheless, as a result of this strategy isn’t an choice for this query, it’s not an accurate reply right here.

Conclusion. The proper reply is choice E.




Please enter your comment!
Please enter your name here

Most Popular

Recent Comments