Abstract Interface Enumeration in Java

Abstract Interface Enumeration in Java:

Even though java supports the eight predefined built-in primitive types and many nonprimitive types, but still can not cover all possible types of classes or situations.To support this java provides enum(Short of Enumerated types).

An object that implements the Enumeration interface provides a way to access a set of objects sequentially. The Enumeration object hides the actual organization of the set of objects from the code that is using it. An Enumeration can iterate through, or enumerate, its set of objects one at a time. A specific implementation of the interface controls the order in which the objects are presented.

The following is an example of how an Enumeration is used. The example shows a method for printing the values in an Enumeration:

void printAll(Enumeration e) {
while ( e.hasMoreElements() ) {

An Enumeration can be used only once: it iterates through its collection of objects in one direction and cannot be reset or rewound.

Normally, an Enumeration is not instantiated directly but instead returned by a method that needs to enumerate a set of values. For example, the elements() method of the Vector class returns an Enumeration of the elements in the Vector. By the same token, the elements() and keys() methods of the Hashtable class return Enumeration objects for the keys and values in the Hashtable.

It is a special type of class. An enum is a type of fixed list of possible values. The values need to be defined when the enum is created. An enum is not a primitive data type. Each enumerated type constant is a public final static member variable.

See also  Amazing 5 Minutes Guide on Polymorphism in Java

The value of the variable is an object belonging to the enumerated class. There is one such object for each enumerated type constant and there are the only objects of the class that can ever be created.

It is the objects that represent the possible values of the enumerated type. An object that implements the Enumeration interface generates a series of elements, one at a time. The enumerated type constants are variables that refer to these objects.

Successive calls to the nextElement() method which returns the successive elements of the Enumeration series, methods are provided to enumerate through the elements of a vector, the key of a hashtable and the values in a hashtable. Enumerations are also used to specify the input streams from a sequence input stream.

Any class that contains methods of these names(written below) is said to implement the Enumeration interface. We can say the StringTokenizer class implements this interface. The enum type values are referred to as enum constants hence the values are made up with uppercase letters. This is a style but not syntax.

for printing all elements of a vector myVec:

interface Enumeration{
public boolean hasMoreElements();
public Object nextElement();
for(Enumeration e=myVec.elements();

The vector, Hashtable and HashMap classes all have an elements method which returns an Enumeration Objects. The enum definition should not be part of any function including the main method. This has to be defined outside of the method. This is a popular Interface example in java.

The interface Enumeration looks below:

public interface java.util.Enumeration{
public abstruct boolean hasMoreElements();//returns true if the enumeration contains more elements and returns false if the enumeration contains no elements
public abstruct Object nextElement();//returns the next element of this enumeration. It can throw NoSuchElementException if there is not element present.

The details of the class structure are given as follows:

public abstruct boolean hasMoreElements();

public abstruct boolean hasMoreElements() method returns true if the nextElement() method of this Enumeration returns an object the next time it is called.

See also  Variables in Java

This method returns true if the there are more objects to retrieve; false otherwise.

public abstruct Object nextElement();

public abstruct Object nextElement() method returns the next object in the set of objects encapsulated by this Enumeration.

This method returns the next object in this Enumeration.

An Example:

Vector v=new Vector();
Enumeration enum=v.elements();
while (enum.hasMoreEmenets())
String s=(String)enum.nextElement();

An enum is technically a class so the enum values are objects of that class. They also contain methods called ordinal(). When used with an enum, the ordinal() method returns the position of the enum object in the list.

enum Browser{IE,FF,Chrome}
Browser browserIE=Browser.IE;
Browser browserFF=Browser.FF;
Browser browserChrome=Browser.Chrome;
Browser.FF.ordinal();//returns 2 

A unit of collection of data items is called a data structure. Example List(a sequence of items)
enhanced for loop can be used as below:

for((enum-type-name)(variable name):(enum-type-name).values()){{
enum Days{Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday}
for(Days d:Days.values())

Enhanced for loop is to do something “for each” item in a data structure, it is often called for each loop.like for each Days d in Days.values(): as in.
The method clone() duplicates the class vector, not the objects it holds. Since clone is a method of Object class, many classes are cloneable.

Another Example:

class EnumeratorTest{
public static void main(String args[]){
Vector v=new Vector();
v.addElement(new file("."));
v.addElement(new Vector());
v.addElement(new EnumeratorTest());
v.addElement(new Date());
//class object represents a class itself.
Hashtable h=new Hashtable();
h.put(new Integer(1),new Thread());
h.put(new Integer(2),new ProtocolException().getClass().getSuperClass());
h.put(new Integer(3),v.clone());

Note that the only thing common between containers is that they contain the only class object. Now if we to keep a specific type in that case Enumeration is the key. However, we need to downcast Object to the particular type. This is a popular Interface example in java.

Share and Enjoy !

Leave a Reply

Your email address will not be published. Required fields are marked *