Monday, November 28, 2022
HomeJavaQuiz your self: The reality about Java enums

Quiz your self: The reality about Java enums


Java enums, Oracle Java Exam, Oracle Java Prep, Oracle Java Career, Oracle Java Skills, Oracle Java Skills, Oracle Java Prep

Which statements about enums are appropriate? Select two.

A. You can not declare a remaining technique within the enum kind.

B. You may declare an summary technique within the enum kind.

C. enum cases are immutable.

D. You may override the equals technique to implement your individual comparability primarily based on an enum fixed’s state.

E. enum members with protected entry modifiers can’t be accessed outdoors an enum’s personal package deal.

Reply. The aim of the enum idea is to offer a particular variety of cases of a given kind. That quantity is mounted within the supply code, and every occasion has a well-defined title within the code.

Enums have a typical father or mother class. It’s prohibited to explain one other class as extends any enum. This rule ensures that no different class may be outlined from which objects may be created which are assignment-compatible with the enum kind. In spite of everything, should you have been allowed to outline new courses suitable with the enum kind, you can then arbitrarily add new objects past the meant set of cases.

As well as, controls are imposed on any enum to make sure that deserialization and different behaviors don’t create new, unintended cases.

Supporting these concepts is likely one of the results of getting the widespread father or mother class—however be aware that the enum should not declare that it extends that father or mother class. As a substitute, this occurs mechanically due to the usage of the enum key phrase instead of the category key phrase.

These preliminary concepts have some penalties which are related to figuring out the reply to this query.

Possibility A suggests that you simply can’t declare a remaining technique in an enum kind. At first look doing so might sound pointless, because the enum appears to be remaining. Nevertheless, you may, the truth is, outline subtypes of the enum, supplied you achieve this instantly inside the enum kind itself, within the context of one of many cases.

It’s most likely useful to emphasise the excellence between the enum kind and the cases of the enum.

Contemplate the pattern code under; the enum kind known as Kind has two cases: One is referred to utilizing the reference GOOD, and the opposite is referred to utilizing the reference BAD. Nevertheless, the syntax used right here, the place GOOD is adopted by curly braces, truly implies that GOOD refers to an occasion of an nameless subtype of Kind. On condition that the instance defines the doIt() technique within the enum kind Kind as being remaining, this prevents overriding of that technique in GOOD.

enum Kind {

    GOOD {}, // can’t override doIt right here

    BAD;

    remaining void doIt() {} // OK

}

Since subclassing the enum is feasible, though solely underneath these very particular syntactic constraints, it’s not unreasonable to declare a way as remaining, and it’s, the truth is, permitted. From this, it’s clear that choice A is inaccurate.

Now that you recognize that restricted subtyping is feasible with enum varieties, it’s affordable to count on which you can usefully declare an summary technique inside an enum kind, and that expectation is appropriate. In such a state of affairs you will need to present an implementation of the summary technique inside every enum fixed’s physique. You need to not go away any summary technique unimplemented, as a result of the enum kind could not itself be declared as summary.

enum Kind { // can’t have summary modifier

    GOOD {

        String describe() { return “Good”; }

    },

    BAD {

        String describe() { return “Not good”; }

    };

    // that is legitimate supplied all of the above objects implement

    // the strategy 

    summary String describe(); 

}

From this you recognize that choice B is appropriate.

An enum ensures a particular variety of cases of the sort however doesn’t assure immutability. Guaranteeing immutability is the accountability of the developer. An enum fixed is known as a public static remaining reference to an occasion of the enum kind. Whereas that reference can’t be modified, the article to which it refers is likely to be mutable. This could occur if the enum is asserted in such a approach that it comprises a mutable area, and the next code illustrates this:

enum Kind {

    GOOD,

    BAD;

    // description isn’t remaining, so it may be modified

    public String description; 

    String describe() { return description; }

}

On this instance, the general public String area named description may very well be reassigned to consult with a unique String object. Equally, if the sector have been declared to be of the StringBuilder kind, the contents of that object can be intrinsically mutable. From this you may see that choice C is inaccurate.

Possibility D can also be incorrect: Every enum kind implicitly inherits from the java.lang.Enum class, which already has some remaining strategies, particularly, the next:

◉ compareTo()

◉ equals()

◉ hashCode()

◉ title()

◉ ordinal()

As a result of the equals() technique is remaining within the base kind Enum, you can not override it, and you can not implement your individual comparability logic. The inherited implementation seems like this.

public remaining boolean equals(Object different) {

  return this==different;

}

Possibility E is appropriate, as protected members of Java courses (together with enums) may be accessed solely in a unique package deal if the entry is carried out in a subtype that’s declared in that different package deal.

Nevertheless, subclasses of an enum should be nested, that’s, declared contained in the top-level curly braces that include the physique of the enum declaration. Due to that, the subtypes of an enum can’t probably be in a unique package deal. Consequently, all protected members may be accessed solely from the enum’s package deal and never from exterior packages. Their entry is, due to this fact, indistinguishable from what it could be in the event that they have been declared with default accessibility, that’s, with none specific modifier.

Conclusion. The right solutions are choices B and E.

Supply: oracle.com

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments