Enums with Methods
In Java, enums are more than just collections of constantsβthey can also contain methods, fields, and even abstract methods that each constant must implement. By adding methods to an enum, you can give each constant its own behavior, making your code more flexible and easier to maintain. One powerful technique is using constant-specific class bodies, which allow you to override methods for individual constants, much like subclassing. This is especially useful when you want each enum constant to behave differently for a shared operation.
Operation.java
12345678910111213141516171819202122232425262728293031package com.example; public enum Operation { PLUS { @Override public double apply(double x, double y) { return x + y; } }, MINUS { @Override public double apply(double x, double y) { return x - y; } }, TIMES { @Override public double apply(double x, double y) { return x * y; } }, DIVIDE { @Override public double apply(double x, double y) { return x / y; } }; public abstract double apply(double x, double y); }
By using constant-specific class bodies, you can give each enum constant its own implementation of a method. This approach is closely related to the strategy pattern in object-oriented design, where you define a family of algorithms, encapsulate each one, and make them interchangeable. With enums, you can bundle related behaviors directly with the constants, reducing the need for external classes or switch statements and making your code more concise and expressive.
EnumMethodDemo.java
12345678910111213141516171819202122232425262728293031323334353637383940package com.example; public class Main { public static void main(String[] args) { double x = 10.0; double y = 5.0; for (Operation op : Operation.values()) { System.out.println(op + ": " + op.apply(x, y)); } } } enum Operation { PLUS { @Override public double apply(double x, double y) { return x + y; } }, MINUS { @Override public double apply(double x, double y) { return x - y; } }, TIMES { @Override public double apply(double x, double y) { return x * y; } }, DIVIDE { @Override public double apply(double x, double y) { return x / y; } }; abstract double apply(double x, double y); }
1. What is a constant-specific class body in an enum?
2. Can enums have abstract methods?
3. When would you use methods inside an enum?
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you show an example of how to implement an enum with methods in Java?
How does using methods in enums compare to using switch statements?
What are some real-world scenarios where this enum strategy pattern is useful?
Awesome!
Completion rate improved to 11.11
Enums with Methods
Swipe to show menu
In Java, enums are more than just collections of constantsβthey can also contain methods, fields, and even abstract methods that each constant must implement. By adding methods to an enum, you can give each constant its own behavior, making your code more flexible and easier to maintain. One powerful technique is using constant-specific class bodies, which allow you to override methods for individual constants, much like subclassing. This is especially useful when you want each enum constant to behave differently for a shared operation.
Operation.java
12345678910111213141516171819202122232425262728293031package com.example; public enum Operation { PLUS { @Override public double apply(double x, double y) { return x + y; } }, MINUS { @Override public double apply(double x, double y) { return x - y; } }, TIMES { @Override public double apply(double x, double y) { return x * y; } }, DIVIDE { @Override public double apply(double x, double y) { return x / y; } }; public abstract double apply(double x, double y); }
By using constant-specific class bodies, you can give each enum constant its own implementation of a method. This approach is closely related to the strategy pattern in object-oriented design, where you define a family of algorithms, encapsulate each one, and make them interchangeable. With enums, you can bundle related behaviors directly with the constants, reducing the need for external classes or switch statements and making your code more concise and expressive.
EnumMethodDemo.java
12345678910111213141516171819202122232425262728293031323334353637383940package com.example; public class Main { public static void main(String[] args) { double x = 10.0; double y = 5.0; for (Operation op : Operation.values()) { System.out.println(op + ": " + op.apply(x, y)); } } } enum Operation { PLUS { @Override public double apply(double x, double y) { return x + y; } }, MINUS { @Override public double apply(double x, double y) { return x - y; } }, TIMES { @Override public double apply(double x, double y) { return x * y; } }, DIVIDE { @Override public double apply(double x, double y) { return x / y; } }; abstract double apply(double x, double y); }
1. What is a constant-specific class body in an enum?
2. Can enums have abstract methods?
3. When would you use methods inside an enum?
Thanks for your feedback!