Monday, July 22, 2024
HomeJavaQuiz your self: The allowable subtypes in sealed courses

Quiz your self: The allowable subtypes in sealed courses

Core Java, Oracle Java Certification, Oracle Java Exam, Oracle Java Preparation, Oracle Java Career, Java Skills, Java Jobs

It is best to know what the foundations say about subclasses being public, protected, and remaining.

Given the sealed Hen class

public sealed class Hen permits Sparrow {}

Which Sparrow class definitions, when used independently, are legitimate? Select two.

A. non-sealed class Sparrow extends Hen { }

B. public class Sparrow extends Hen { }

C. protected class Sparrow extends Hen { }

D. remaining class Sparrow extends Hen { }

Reply. The conventional syntax for a sealed kind requires that the kind outline within the permits part the listing of allowed direct subtypes. On this query, the Hen class enumerates just one allowed direct subclass, which is Sparrow.

The direct subtypes of a sealed kind are topic to some constraints; they have to carry one of many following three listed modifiers, which trigger the consequences famous:

◉ non-sealed, wherein case the non-sealed kind can itself have arbitrary subtypes

◉ sealed, wherein case this subtype should additionally declare a nonempty permits clause enumerating no less than one existent subtype

◉ remaining, wherein case the kind should be a concrete class and no additional subclasses are allowed

Within the code of the query, solely choices A and D fulfill these necessities; thus, these choices are right.

Possibility B could be legitimate as an everyday subclass of a Hen if the Hen class weren’t sealed. Since Hen is sealed, nevertheless, Sparrow is unacceptable as a result of it fails to fulfill the constraints listed above. From this, you already know that possibility B is wrong.

Possibility C can also be incorrect as a result of the constraints listed usually are not met: The protected modifier doesn’t fulfill the constraints. As well as, within the query it seems that the courses listed are all top-level courses, and the protected modifier could also be utilized solely to fields, strategies, constructors, or nested sorts, to not top-level sorts.

Conclusion. The right solutions are choices A and D.




Please enter your comment!
Please enter your name here

Most Popular

Recent Comments