You are on page 1of 2

public enum WeekDay

{
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;
}

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.

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 we declare Constructor inside Enum in Java?


-Yes , you can only declare either private or package-private constructor inside
enum.
-public and protected constructors are not permitted inside enum.

Q.Can Enum extend a class in Java?


-No Enum cannot extends class.
-Since all Enum by default extends abstract base class java.lang.Enum,obviously
they can not extend another class, because Java doesn't support multiple
inheritance for classes.

Q. Can constuctor be invoked using an Enum?


A: Yes we can use the constuctor with the name of Enum.

Q: Can we extend an Enum to add elements?


A: No, we cannot extend Enum further in the Code.

Q: Can we create object of Enum?


No

Q: What are advantages of using Enum in Java?


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

Q: What does ordinal() method do in Enum?


-Ordinal method returns the order in which Enum instance are declared inside Enum.
For example in a DayOfWeek Enum, you can declare days in order they come e.g.
public enum WeekDay{
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;
}
-If we call WeekDay.Thursday.ordinal() it will return 3.

Q: Can Enum implement interface in Java?


Yes

Q. Can we use Enum with TreeSet or TreeMap in Java?


-Enum implements Comparable interface, which is the main requirement to be used in
Sorted Collection like TreeSet and TreeMap.
-Since Enum by default impalement Comparable interface, they can be safely used
inside TreeSet or TreeMap in Java.

Q.What is difference between equal() and == in Enum.


-We can use both == and equals() method to compare Enum, they will produce same
result because equals() method of Java.lang.Enum internally uses == to compare enum
in Java.
-Since every Enum in Java implicitly extends java.lang.Enum ,and since equals()
method is declared final, there is no chance of overriding equals method in user
defined enum.

https://www.javainuse.com/misc/enum-interview-questions

You might also like