Pass Enum Parameter Interface Java

Pass Enum Parameter Interface Java

In this example you will see how to pass Enum to the interface abstract method and we will also create class which will implement the interface, implements its method which take Enum as parameter and print its value:

  • EnumTest.java:
public enum EnumTest {
	
    NotAffirmed("Not Affirmed", 0),
    NotEligible("Not Eligible", 1),
    Affirmed("", 3),
    Rejected("", 4),
    Withdrawn("", 5);

    private final int value;
    private final String description;

    EnumTest(String description, int value) {
        this.description = description;
        this.value = value;
    }

    public int  getValue() {
        return value;
    }

    public String getDescription() {
        return description;
    }

    
}
  • InterfaceTest.java:
public interface InterfaceTest {
	
	EnumTest getValue();
	void setValue(EnumTest enumTest);
}

ClassTest.java:

public class ClassTest implements InterfaceTest{
	
	private EnumTest enumTest;

	public static void main(String[] args) {
		
		ClassTest classTest = new ClassTest();
		
		classTest.setValue(EnumTest.NotEligible);
		System.out.println(classTest.getValue().getValue());

	}

	public EnumTest getValue() {
		return enumTest;
	}

	public void setValue(EnumTest enumTest) {
		this.enumTest = enumTest;
		
	}
	
	

}

Reference:

Leave a Reply

Your email address will not be published. Required fields are marked *