A record type is always implicitly final, so it cannot be extended by either a regular class or a record.
You’re designing an IoT device controlled by Java. The device has two modes, one for day and one for night. The modes differ in terms of configuration data, and that data should be constant, so you have decided to use Java records for the implementation. Further, since no other mode types are permitted, you have decided to seal the mode hierarchy something like this, but have not decided what the type of should be used to implement the parent of the sealed Mode hierarchy:
Read More: 1Z0-829: Oracle Java SE 17 Developer
public sealed <type> Mode permits DayModeRecord, NightModeRecord {
}
Which statement is true? Choose one.
A. The Mode type must be a class.
B. The Mode type must be a record.
C. The Mode type must be an interface.
D. The Mode may be either a class or an interface.
Answer. Generally, a sealed-type hierarchy can have a class or an interface as its root. The remainder of the hierarchy can contain classes or interfaces, provided all leaf nodes of the hierarchy are either final concrete classes or are non-sealed.
If a leaf element is non-sealed, it can be either a class or an interface.
However, a record is prohibited from using an extends clause, because all record types have java.lang.Record as their parent, and this cannot be made explicit. Consequently, a record cannot inherit from any user-selected class. Therefore, making Mode a class would prevent the two mode records from inheriting from the base. From this, you know that the Mode type must not be a class; therefore, option A and option D are incorrect.
Option B suggests that the Mode type should be a record. However, a record type is always implicitly final, so it cannot be extended by either a regular class or a record. Because of this, option B must be incorrect.
You’ve seen that an interface is permitted for the root (and potentially some further elements) in a sealed-type hierarchy, and although a record is prohibited from explicitly extending anything, records are permitted to implement interfaces. Option C proposes making the Mode type an interface, and it’s clearly not only a valid approach, but it’s the only valid way to create a sealed-type hierarchy where a leaf node in the sealed hierarchy is a record. From this, you can see that option C is correct.
Conclusion. The correct answer is option C.
Source: oracle.com
0 comments:
Post a Comment