Enumerators
Today we will be looking at enumerators in Java. Enumerators, or “Enums” are a special type of Java class. They are used to define a group of constants in your applications.
In addition to holding constants, enums can also contain methods which can be useful for testing and using your enumerator effectively.
Below is a simple enumerator example for difficulty levels:

It defines three difficulty levels; easy, medium and hard. We can then refer to the constants like so:

This method of referencing constants can then be used in conditional statements that we’ve seen before, to check whether our difficulty matches a given type. This can then change the behaviour of the application.
Enumerators can be used for a variety of things, including creating special constant types, defining actions in an order.




This could be useful for defining the behaviour of “AI” in your applications, or for just defining constants for you to use.
There is one more type of class that we will cover in this series: interfaces.
Interfaces
Interfaces provide an alternative method for abstraction in Java, but unlike abstract classes, interfaces are completely abstract. This means that methods are public and abstract by default. In addition, and fields are public, static and final by default.
They are used to group related functions together with an empty body.

Accessing of these interfaces is different to the abstract classes as well. Instead of the “extends” keyword, interfaces require the “implements” keyword.

On the implementation of an interface, you will be required to override all of the methods in the interface, providing your own implementations for those methods.
A further difference between interfaces and abstract classes is that interfaces cannot contain a constructor.
What benefits are there to interfaces then? Well interfaces can be used to implement extensible functionality. Whilst it is much like an abstract class, interfaces are probably better for use in reflection, as they enforce the overriding of their functions, which means user made classes will definitely behave in the way the user has designed.
Next Time
Next time we will be looking at serialization, in particular saving and loading objects and objects that contain other objects. This will invole creating a new kind of file reader and writer.