Tuesday, May 7, 2024
HomeJavaQuiz your self: Sealed and non-sealed courses and interfaces

Quiz your self: Sealed and non-sealed courses and interfaces


Oracle Java, Java Certification, Java Career, Oracle Java Preparation, Java Tutorial and Material, Oracle Java Tutorial and Material

Given the next code

sealed interface Tremendous permits Sub1, Sub2, Sub3 {}

non-sealed class Sub1 implements Tremendous {}

document Sub2(Tremendous s) implements Tremendous {}

enum Sub3 implements Tremendous { SUB_A{}, SUB_B{} }

Which assertion is true? Select one.

A. The code compiles as it’s.

B. The document Sub2 is invalid.

C. The enum Sub3 is invalid.

D. Each document Sub2 and enum Sub3 are invalid.

Reply. The aim of sealed varieties is to allow you to restrict the set of varieties that may be assignment-compatible with a selected base sort.

A typical instance of inheritance could be a base sort Animal with youngster courses for Lion, Tiger, Bear, and so forth. This construction makes excellent sense for those who resolve so as to add Snake, Canine, Chicken, and any variety of extra varieties which are subtypes of Animal.

Generally, nonetheless, you would possibly have to mannequin a state of affairs the place there are a restricted variety of attainable or permissible subtypes. An instance could be a base sort Quadrilateral (four-sided geometric form). The attainable subtypes of this may be Sq., Rectangle, Parallelogram, Trapezium, Trapezoid, Rhombus, and Kite. (Let’s not get choosy in regards to the geometric definitions, as we recall that two of these have completely different meanings on reverse sides of the Atlantic Ocean!)

There are good causes (a few of that are associated to the type of change/case building that’s a preview characteristic in Java 17 and Java 18) why it may be useful to the integrity of the design to have the ability to implement this restriction and never permit any extra subtypes. It’s past this text’s scope to debate these causes, however it’s adequate to know that the sealed class syntax is offered to will let you implement this.

To make the sealed class syntax work, the syntax should will let you outline a base sort (which might be an interface, summary class, or concrete class) and enumerate the permitted subtypes. Additional, the syntax should implement controls on the subtypes of these subtypes. This aim suggests the next guidelines:

◉ A sealed sort should (usually—however this text is not going to get into the exception) explicitly declare the permitted subtypes.

◉ Every permitted subtype should both be sealed or closing (by which case it can’t have any subtypes).

◉ It’s additionally permitted to have a subtype that’s explicitly declared as non-sealed, by which case it permits any variety of subtypes.

All sealed courses have some interactions with current sort guidelines. For instance, summary courses and interfaces can’t be closing, in order that risk is eliminated and both of them might be sealed or non-sealed, however they can’t be closing.

You possibly can most likely guess that enums, which prohibit arbitrary subclassing, and document varieties, that are implicitly closing, additionally work together with these guidelines. That will probably be explored shortly.

Now that you simply’ve acquired a very good place to begin, think about the weather offered on this query.

The declaration of Tremendous is legitimate. An interface might be sealed, the syntax is sweet, and the declaration enumerates the three permitted subtypes: Sub1, Sub2, and Sub3.

The declaration of the category Sub1 can be legitimate. It demonstrates the usage of the non-sealed thought. It’s permitted to have arbitrary subtypes, and it’s express about that. Thus it’s appropriate.

The document Sub2 declaration can be legitimate. It’s not labeled closing, sealed, or non-sealed, however a document is implicitly closing, so it’s acceptable in that respect. Additionally, though a document can’t declare a mum or dad class, it’s wonderful for a document to implement an interface, as this one does. You may add the ultimate modifier explicitly, however that isn’t required, so the code for Sub2 compiles.

The Sub3 enum is attention-grabbing. In contrast to the document Sub1, an enum isn’t essentially closing. It’s true that you may’t use extends from an enum, however you possibly can create subclasses for those who accomplish that contained in the enum itself—and the code within the query does simply that.

Each the fixed values SUB_A and SUB_B are adopted with curly brace pairs, and which means every occasion is a subclass of the Sub3 enum. As a result of the subtypes of an enum are so tightly managed, the Java Language Specification treats an enum that accommodates kids on this means as being implicitly sealed. Part 8.9 of the specification for Java 17 says

An enum class is both implicitly closing or implicitly sealed, as follows:

◉ An enum class is implicitly closing if its declaration accommodates no enum constants which have a category physique.

◉ An enum class E is implicitly sealed if its declaration accommodates not less than one enum fixed that has a category physique. The permitted direct subclasses of E are the nameless courses implicitly declared by the enum constants which have a category physique.

Within the state of affairs the place no class our bodies have been offered for any of the person enum constants, the enum class could be implicitly closing and would work too.

As a result of the Sub1 class is explicitly marked non-sealed, the document Sub2 is implicitly closing, and the enum Sub3 is implicitly sealed, all three are legitimate and the syntax of the code offered is completely appropriate and compiles. In view of that, you possibly can see that choice A is appropriate, and due to this fact choices B, C, and D should be incorrect.

Conclusion. The proper reply is choice A.

Supply: oracle.com

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments