Top Java Enum frequently asked interview questions
In this post we will look at Enum Interview questions. Examples are provided with explanation.
Q: Why do we have the concept of enum?
A: Enum is the lists of constants, so it is useful when required to define a list of constants. Enum is useful when list of outcomes is limited.
Q: Can Enum constants can be declared as static and final?
A: Yes. Enum constants are public static and final and are accessible directly using enum name.
Q: Can a constuctor be invoked using an Enum?
A: Yes we can use the constuctor with the name of Enum.
Enum Color { RED,GREEN } Color() { System.out.println("This is constuctor"); }
Q: Can we extend an Enum to add elements?
A: No, we cannot extend Enum further in the Code. It is defined only with keyword Enum and filled with elements but these elements cannot be added further in the program using some alternative method.
Q: Can we create object of Enum?
A: No, it is not possible to create the object of Enum.
Q: What are advantages of using Enum in Java?
A: 1.) Enum improves safety
2.) Enum can be placed in or out of a class
3.) Enum can be easily used in switch case
4.) Enum cannot extend any class
5.) Object of enum cannot be made
6.) Enum can have variables, constructors and methods