enums are automatically Comparable, there is no need to explicitly add the "implements Comparable".
When we check the enum weather it is instance of Comparable, it return true.
enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY;
}
public class EnumTest {
public static void main(String[] args) {
Day day = Day.MONDAY;
String isComparable = "Not Comparable";
if(day instanceof Comparable) {
isComparable = "Comparable";
}
System.out.println(isComparable);
}
}
Output: Comparable
No comments:
Post a Comment