79822751

Date: 2025-11-17 20:40:37
Score: 1
Natty:
Report link

Why do enums sometimes use abstract methods? (Simple Explanation)

In Java, an enum is not just a list of values — it’s actually a special kind of class.
Because of that, each enum constant can have its own behavior.

So, if you add an abstract method inside an enum, every constant must provide its own implementation.
This lets each enum value act differently.

enum Operation {
    PLUS {
        @Override
        double apply(double x, double y) { return x + y; }
    },
    MINUS {
        @Override
        double apply(double x, double y) { return x - y; }
    };

    abstract double apply(double x, double y);
}

What’s happening here?


Why is this useful?

Example:
If you want to add MULTIPLY, you just add it and implement its version of apply().


Simple summary

Abstract methods in enums let each enum value have its own behavior.
It’s a clean way to use polymorphism without creating many separate classes.

Reasons:
  • RegEx Blacklisted phrase (0.5): Why is this
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Starts with a question (0.5): Why do
  • Low reputation (1):
Posted by: Youssef Amjad