Java Challenge #8: Enums Implementing Interfaces

Enums, do you really master them? Can you tell me if it’s going to compile? If not, explain why. If so, write the result.

Try to solve this challenge before seeing the answer below.

enum.PNG

Answer: The first thing to observe is the implementation of the interface Invoker on the Enum. It’s not very common to do this, but it’s possible. When we access the Enum reference of the reference it does not make a difference. The invokeShenLong() method will be invoked in the same way bringing all the Enums declared on DragonBalls. When we enter the loop, we are not using brackets ({}) on the “if” command. This means that only one line will be executed.

Conclusion: Until the Enums are equaled, the looping will continue. In the end, the iterated Enums will be FIFTH and equal to one another.

The output will be:
Same dragon ball
FIFTH

Written by
Rafael del Nero
Join the discussion

Stand Out as a Java Developer and Get Your Dream Job!

You will get the book by email in a few minutes after registering.