Enum Interface in Java

Hello friends how are you, today in this blog i will teach you what is enum interface, how to create enum interface,  how to store elements in enum interface,how enum interface works and many programs using enum interface in a very simple way. 

Let's start
  • It is a predefined interface.
  • It is used to access/retrieve the data from any collection like vector ,stack etc.
  • There are two main methods of enumeration interface
  • 1. hasMoreElements()
  • It returns boolean value(true/false).
  • It returns true if the enumeration contains atleast one or more than one element otherwise returns false.
  • 2. nextElement()
  • It returns the next element of enumeration.
  • Example

import java.util.Vector;
import java.util.Enumeration;
 class Program 
 {
   public static void main(String args[]) 
   {
      /*creating variable of enumeration*/
      Enumeration courses;
      /*creating object of vector*/
      Vector courseName = new Vector(); 
      /*adding data into vector*/
      courseName.add("C");
      courseName.add("C++");
      courseName.add("JAVA");
      courseName.add("PHP");
      courseName.add("ANDROID");
      courseName.add("C#");
      /*passing vector data into enumeration*/
      courses = courseName.elements();
      /*Accessing data of enumeration*/
      while (courses.hasMoreElements()) 
      {
       /*printing data of enumeration*/
       System.out.println(courses.nextElement()); 
      }
   }
}
/*
Output
C
C++
JAVA
PHP
ANDROID
C#
*/
In the above program we can see that we are importing two packages one for enumeration and the second one for vector. In the main function we have created variable of Enumeration which is courses. After that  we have created object of class vector which is courseName and stored many programming languages in the vector and after storing elements into it we have run a loop to access all the elements of Enumeration.

Request:-If you found this post helpful then let me know by your comment and share it with your friend. 
If you want to ask a question or want to suggest then type your question or suggestion in comment box so that we could do something new for you all. 

If you have not subscribed my website then please subscribe my website. 

Try to learn something new and teach something new to other. 

Thanks.😊

Post a Comment

0 Comments