Java Questions 21 - 30  «Prev  Next»

Java Enumerated Types - Interview Questions

  1. What is the purpose of a constant specific Class Body?

    Answer:
    You can define a construct in an enum that looks like an anonymous inner class. It is known as a constant specific class body, and you use it when you need a particular constant to override a method defined in the enum. This allows you to map a specific size to each constant.
    enum CoffeeSize{               
     BIG(8),   
     HUGE(10),   
     OVERWHELMING(16) {   
      public String getLidCode(){ return "A";   }   
     };   
     private int ounces;   
    
     public int getOunces(){   
      return ounces;   
     }    
     CoffeeSize(int ounces){   
      this.ounces = ounces;   
     }   
     public String getLidCode(){   
      return "B";   
     }   
    }  
    

    A Constant Specific Class Body is what the JLS refers to as an optional class body for an enum constant.
    It is implemented as an anonymous inner class that extends the outer, enclosing enum. In the above example, the enum constant OVERWHELMING will be of an anonymous inner type that extends CoffeeSize, and overrides the method getLidCode().
    The inner class looks, in pseudocode, looks something like this:
    class CoffeeSize$1 extends CoffeeSize {
     @Override
      public String getLidCode() {
       return "A";
      }
    }
    


  2. Can an interface extend another interface?

    Answer:
    Yes, an interface can extend another interface but cannot implement another interface.
    Implements denotes defining an implementation for the methods of an interface. Because an interface does not have an implementation, an "interface cannot implement another interface".
  3. What must a class do when it implements an interface?

    Answer:
    Any class that implements an interface must implement all methods from all the interfaces in the inheritance tree of the interface the class is implementing.

  4. public final class ConfigurationService {
      private static final ConfigurationService INSTANCE = new ConfigurationService();
      private List providers;
      private ConfigurationService() {
       providers = new ArrayList();
      }
    
      public static void addProvider(ConfigurationProvider provider) {
       INSTANCE.providers.add(provider);
      }
    }
    

    INSTANCE is declared as final. Why can objects be added to INSTANCE?

    Answer:
    final simply says that the object reference is unchangeable. The object it points to is not immutable through this operation.
    INSTANCE can never refer to another object, but the object it refers to may change state.
    A reference variable marked final can never be changed but the object it refers to can be modified.

  5. What do the 3 dots in the following method mean?
    public void testMethod(String... strings){
     // method body
    }
    


    Answer:
    As of Java 5, methods can be declared with a var-arg parameter.
    a) Methods can declare a parameter that accepts from zero to many arguments, a so-called var-arg method.
    b) A var-arg parameter is declared with the syntax type... name; for instance:
    doSomething(int... x) { }
    

    c) A var-arg method can have only one var-arg parameter.
    d) In methods with normal parameters and a var-arg, the var-arg must come last.

  6. What is the relationship between a static method and instance variable?

    Answer:
    A static method can not directly access an instance variable from the class it is in.
    A static variable can be accessed directly by the class name and does not rely on the creation of an instance.

  7. What is the benefit of using an enum instead of a constant?


    Answer:
    Example:
    In prior releases, the standard way to represent an enumerated type was the int Enum pattern:
    // int Enum Pattern contains several problems
    public static final int SEASON_WINTER = 1;
    public static final int SEASON_SPRING = 2;
    public static final int SEASON_SUMMER = 3;
    public static final int SEASON_FALL   = 4;
    

    This pattern has many problems, such as:
    1. Not typesafe: Since a season is just an int you can pass in any other int value where a season is required, or add two seasons together (which does not make sense).
    2. No namespace: You must prefix constants of an int enum with a string (in this case SEASON_) to avoid collisions with other int enum types.
    3. Brittleness: Because int enums are compile-time constants, they are compiled into clients that use them. If a new constant is added between two existing constants or the order is changed, clients must be recompiled. If they are not, they will still run, but their behavior will be undefined.
    4. Printed values are uninformative: Because they are just ints, if you print one out all you get is a number, which tells you nothing about what it represents, or even what type it is.
    An enum is a much safer and more flexible way to implement constants than was possible in earlier versions of Java .

  8. What must JavaBeans methods start with ?

    Answer:
    JavaBeans methods must start with
    1. _set
    2. get
    3. is
    4. add
    5. remove

  9. In which section of code are you prohibited from declaring enums?

    Answer:
    Enums cannot be declared inside of a method.

  10. What is bytecode?

    Answer:
    Bytecode is translated by the operating system specific Java Virtual Machine (JVM) into the platform specific machine code. Role of Interpreter