Learnitweb

enum in Java – constructor, methods and interface

enum with constructor

Java enum can have a constructor to pass data while creating enum constants. For example, in case of seven days of a week, we can pass 1 to Sunday, 2 to Monday and so on. You can also provide one or more constructor to your enum as enum supports constructor overloading.

Constructor in enums can only be either private or package level. It can’t be public or protected.

In the following example, note that after each constant there is a value in parentheses. This syntax calls the constructor for that constant member to initialize the dayNumber field for that member.

public class EnumWithConstructorExample {
	public enum Day {
		SUNDAY(1), MONDAY(2), TUESDAY(3), WEDNESDAY(4), THURSDAY(5), FRIDAY(6), SATURDAY(7);
		private final int dayNumber;
	    
	    Day(int number){
	    	this.dayNumber = number;
	    }	
	    
	    public int getDayNumber() {
	    	return dayNumber;
	    }
	};
	
	public static void main(String args[]) {
		Day sunday = Day.SUNDAY;
		System.out.println(sunday.getDayNumber());
	}
}

Output

Sunday day number: 1

Concrete method in enum

enum in Java can have both concrete and abstract methods. Adding a concrete method in enum is just like adding a method in any other class. You can use public, private or protected access specifier. Methods in enum can return value as well.

public class EnumWithConcreteMethod {
	public enum Day {
		SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;
		public void printDay() {
			System.out.println("Day is : " + this);
		}
	};
	
	public static void main(String args[]) {
		Day sunday = Day.MONDAY;
		sunday.printDay();
	}
}

Output

Day is : MONDAY

abstract method in enum

Yes, you can define abstract methods in an enum declaration if and only if all enum values have custom class bodies with implementations of those methods. In the following example, we have defined Days as enum type and each Day has implemented the printDay() abstract method.

public class EnumAbstractMethodExample {
	public enum Days {
		SUNDAY {
			public void printDay() {
				System.out.println("This is Sunday");
			}
		},
		MONDAY {
			public void printDay() {
				System.out.println("This is Sunday");
			}
		};
		public abstract void printDay();
	}
}

Following is not possible as Sunday has not implemented printDay() method and will result in compile time error – ‘The enum constant SUNDAY must implement the abstract method printDay()’.

public class WrongEnumAbstractMethodExample {
	public enum Day {
		SUNDAY, MONDAY {
			public void printDay() {
				System.out.println("This is Sunday");
			}
		};
		public abstract void printDay();
	}
}

Adding abstract method enforces a contract for all enums to be created.

enum and interfaces

An enum can implement any number of interfaces just like any other class.

interface Print {
	public abstract void printDay();
}

public class EnumInterfaceExample {
	public enum Days implements Print {
		SUNDAY, MONDAY;

		@Override
		public void printDay() {
			System.out.println("Day is : " + this);
		}
	}

	public static void main(String args[]) {
		Days sunday = Days.SUNDAY;
		sunday.printDay();
	}
}

Output

Day is : SUNDAY