Java / Enum
1. Define "enum".
A Java Enum, introduced in Java 5, is a special kind of Java type used to define collections of constants. It is a special class type that holds constants, methods....
2. Why do we need enum?
Enum is nothing but the lists of constants, so it is useful when required to define a list of constants.
3. Can an enum be declared final?
No. Compiler generates sub classes for each constants contained in the enum.
4. Can an enum be declared abstract?
No.
5. Can an enum implement an interface?
Yes. interface IJob { void jobRole (); } public enum Job implements IJob { JOB1( 50 ) { @Override void whoAmI () { System.out.println( "I am a Software Tester."...
6. Can an Enum implement a interface in Java?
Yes, Enum can implement interface in Java. An enum is a type, similar to class and interface, it can implement interface.
7. Can an Enum extend a class in Java?
No, Enum can not extend class in Java. By default, Enum extends abstract base class java.lang.Enum so it cannot extend another class as Java does not support multiple inheritance for classes.
8. Is Enum Serializable in Java?
Yes. Java enums are automatically Serializable, there is no need to explicitly add the "implements Serializable" clause following the enum declaration.
9. Default value for enum's serialVersionUID.
The serialVersionUID value for all Enums is 0L by default.
10. Are Enum constants static and final?
Yes. Enum constants are public static and final and can be accessed directly using enum name.
11. Benefits of using Enums in Java.
Enum provides type-safe ; Enum variable can be assigned only with predefined enum constants otherwise it will issue compilation error. Enum can be used in switch-case Java control statement....
12. Are enums reference type in Java?
Enums are derived type as Java class and interface. Enum can have constructor, methods and variables(fields) and each Enum constant will have its own fields and methods.
13. Can a enum constructor be declared public in Java?
No. Enum constructor cannot be declared as public....
14. Can enum have abstract methods in Java?
Yes. enum can have abstract methods....
15. Can an enum have main method in Java?
Yes. enum can have startup main method that JVM executes....
16. Can a Java enum be subclassed to add new elements?
No. Java enum cannot be extended.
17. What are the implicit methods in Java Enum?
The values and the valueOf method are implicitly added by the compiler. Find the exact method signature below.
18. What is enum ordinal in java?
ordinal method returns the ordinal of this enumeration constant, its position in its enum declaration, where the initial constant is assigned an ordinal of zero. Most developers will have minimal use for this method....
19. Can we create an enum without constants?
Yes. we can create.
20. Can an Enum extend another Enum in Java?
No. All enums implicitly extend java.lang.Enum.