Gadgets

Thursday, 18 February 2016

enum interview questions and answers

enum is introduced in J2SE-5 version, we can represent a group of named constants by using enum. The main objective of enum is to define our own datatypes.

                         enum Week
                         {
                                      SUN,MON,TUS,WED,THU,FRI,SAT;
                         }

1) What is the purpose of values()?
Every enum implicitly contain value() method to list out the values present in enum.

2) What are the ordinal values?
The order of constant is important in enum so we can represent by using ordinal values. By using ordinal() method we can find ordinal values. 

3) Is it possible to define main() method inside enum?
We can define main() method inside enum.

4) Can we declare constructors inside enum?
We can define constructors for enum.
                         enum Week
                         {
                             SUN,MON,TUS,WED,THU,FRI,SAT;
                              Week(){
                                         }
                         }
5) Inheritance concept is applicable for enum?
enum is always final we can’t extend enum, so inheritance concept is not applicable for enum.

6) Is it possible enum implements interfaces?
 enum can implements any number of interface.
                         interface Month
                         {
                         }
                         enum Week implements Month
                         {                     
                         }

     

No comments:

Post a Comment