// enums/cartoons/EnumImplementation.java // (c)2017 MindView LLC: see Copyright.txt // We make no guarantees that this code is fit for any purpose. // Visit http://OnJava8.com for more book information. // An enum can implement an interface // {java enums.cartoons.EnumImplementation} package enums.cartoons; import java.util.*; import java.util.function.*; enum CartoonCharacter implements Supplier { SLAPPY, SPANKY, PUNCHY, SILLY, BOUNCY, NUTTY, BOB; private Random rand = new Random(47); @Override public CartoonCharacter get() { return values()[rand.nextInt(values().length)]; } } public class EnumImplementation { public static void printNext(Supplier rg) { System.out.print(rg.get() + ", "); } public static void main(String[] args) { // Choose any instance: CartoonCharacter cc = CartoonCharacter.BOB; for(int i = 0; i < 10; i++) printNext(cc); } } /* Output: BOB, PUNCHY, BOB, SPANKY, NUTTY, PUNCHY, SLAPPY, NUTTY, NUTTY, SLAPPY, */