Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Enums with Methods | Advanced Enum Features
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Working with Java Enum

bookEnums 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

Operation.java

copy
12345678910111213141516171819202122232425262728293031
package 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

EnumMethodDemo.java

copy
12345678910111213141516171819202122232425262728293031323334353637383940
package 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?

question mark

What is a constant-specific class body in an enum?

Select the correct answer

question mark

Can enums have abstract methods?

Select the correct answer

question mark

When would you use methods inside an enum?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 2

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

bookEnums with Methods

Swipe um das Menü anzuzeigen

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

Operation.java

copy
12345678910111213141516171819202122232425262728293031
package 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

EnumMethodDemo.java

copy
12345678910111213141516171819202122232425262728293031323334353637383940
package 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?

question mark

What is a constant-specific class body in an enum?

Select the correct answer

question mark

Can enums have abstract methods?

Select the correct answer

question mark

When would you use methods inside an enum?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 2
some-alt