Can Enum implement interface in Java?
Yes, Enum can implement interface in Java. Since enum is a type, similar to class and interface, it can implement interface. This gives a lot of flexibility to use Enum as specialized implementation in some cases.
Can Enum extends class in Java?
No, Enum can not extend class in Java. Since all Enum by default extend abstract base class java.lang.Enum, obviously they can not extend another class, because Java doesn't support multiple inheritance for classes. Because of extending java.lang.Enum class, all enum gets methods like ordinal(), values() or valueOf().
How do you create Enum without any instance? Is it possible without compile time error?
yes, you can create Enum without any instance in Java, say for creating a utility class.
Can we override toString() method for Enum? What happens if we don't?
Ofcourse you can override toString in Enum, as like any other class it also extends java.lang.Object and has toString()method available, but even if you don't override, you will not going to regret much, because abstract base class of enum does that for you and return name, which is name of the enum instance itself. here is the code of toString() method from Enum class
Can we create instance of Enum outside of Enum itself? If Not, Why?
No, you can not create enum instances outside of Enum boundry, because Enum doesn't have any public constructor, and compiler doesn't allow you to provide any public constructor in Enum. This enforces declaring enum instances inside Enum itself.
Illegal modifier for the enum constant A; no modifier is allowed
Can we declare Constructor inside Enum in Java?
Yes, you can, but remember you can only declare either private or package-private constructor inside enum. public and protected constructors are not permitted inside enum.
What is difference in comparing Enum with == and equals() method?
you 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.
If you are not just checking whether two enum are equal or not, and rather interested in order of different instance of Enum, than you can use compareTo() method of enum to compare two enums. Java.lang.Enum implements Comparable interface and implements compareTo() method. Natural order of enum is defined by the order they are declared in Java code and same order is returned by ordinal() method.
2) == method provides type safety during compile time
If you are not just checking whether two enum are equal or not, and rather interested in order of different instance of Enum, than you can use compareTo() method of enum to compare two enums. Java.lang.Enum implements Comparable interface and implements compareTo() method. Natural order of enum is defined by the order they are declared in Java code and same order is returned by ordinal() method.
Comparing Enum with equals and ==
1) Using == for comparing Enum can prevent NullPointerException
3) == should be faster than equals method
What does ordinal() method do in Enum?
Ordinal method returns the order in which Enum instance are declared inside Enum.
Can we use Enum in switch case in Java?
Yes, you can use Enum in Switch case in Java, in fact that's one of the main advantage of using Enum. Since Enum instances are compile time constant, you can safely use them inside switch and case statements
for(Language pl : Language.values()){ System.out.println( pl.name() + " : " + pl.rank); }What is advantage and disadvantage of using Enum as Singleton?
http://javarevisited.blogspot.com/2012/07/why-enum-singleton-are-better-in-java.html
What is advantage of using Enum over enum int pattern and enum String pattern?
ou don't have a dedicated enum type, Since it's String variable, which represent day of week, it can take any arbitrary value. Similarly enum int pattern can take any arbitrary value, compiler doesn't prevent those. By using Enum, you get this type-safety and compiler checking for you
Read full article from 15 Java Enum Interview Questions for Developers with Answers | Java67
No comments:
Post a Comment