Abstract Interface Enumeration in Java

digitization, transformation, binary

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.

Ads code goes here

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() ) {
System.out.println(e.nextElement());
}
}

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.

READ  How To Handle Keyboard and Mouse Operation In Selenium Via Action class?

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.

Exp:
for printing all elements of a vector myVec:

interface Enumeration{
public boolean hasMoreElements();
public Object nextElement();
}
for(Enumeration e=myVec.elements();
e.hasMoreElement())
{
System.out.println(e.nextElement());
} 

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{
//methods:
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.

READ  Identifier and Keyword Concept Simplified 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();
v.add("Apple");
v.add("Grapes");
v.add("Banana");
Enumeration enum=v.elements();
while (enum.hasMoreEmenets())
{
String s=(String)enum.nextElement();
System.out.println(s);
}

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()){{
statement
}
//like
enum Days{Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday}
for(Days d:Days.values())
{
System.out.println(d);
System.out.println(d.ordial());
}
 

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 Comment

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