oop
19. Enums are Classes: Constant Specific Logic
Analyze the code, write your predicted output, and then reveal the solution.
Scenario
In Java, enum is much more than a list of constants. Each constant can provide its own implementation of an abstract method.
Code Block
enum Operation { PLUS { double apply(double x, double y) { return x + y; } }, MINUS { double apply(double x, double y) { return x - y; } }; abstract double apply(double x, double y); } public class Main { public static void main(String[] args) { double x = 10, y = 5; for (Operation op : Operation.values()) { System.out.printf("%f %s %f = %f%n", x, op, y, op.apply(x, y)); } } }
Take Your Shot
Predicted Output
Type exactly what you think will be printed in the console.