Friday, October 22, 2010

Constant Specific methods in Enumeration

Using Java 5 Enums,it is possible to define different behaviours for a method based on the value.This is known as Constant Specific Methods.The methods that requires different behavior to be declared as abstract and it should be overriden in each Enum Value.

public enum Game {
CHESS {
@Override
boolean isIndoor() {
return true;
}
},BADMINTON {
@Override
boolean isIndoor() {
return false;
}
},CHINESE_CHECKER {
@Override
boolean isIndoor() {
return true;
}
};

public abstract boolean isIndoor();
}