Accessing Interface Variables in Java

The  variables of an interface are always declared as "final". Final variables are those variables, whose  values are constants and cannot be changed. The class that implements the interface can use the variables as declared in the interface and cannot modify or changed the value of the variable.

 

 interface selectcolor
{
int blue=4;
int yellow=5;
int pink=6;
public void choose(int color);
}
class selectimp implements selectcolor
{
public void choose(int color)
{
switch(color)
{
case blue:
   System.out.println("The color selected is blue");
   break;
case yellow:
   System.out.println("The color selected is yellow");
   break;
case pink:
   System.out.println("The color selected is pink");
   break;
}
}
public static void main(String aa[])
{
int a1,b1,c1;
a1=Integer.parseInt(aa[0]);
b1=Integer.parseInt(aa[1]);
c1=Integer.parseInt(aa[2]);


selectimp st=new selectimp();
st.choose(a1);
st.choose(b1);
st.choose(c1);
}
}

accessible interface variables in java
Explanation....

In this example first  an interface selectcolor is created and the value for the integers blue, yellow, pink are set as 4,5,6.
Then a method choose() which takes an integer parameter is declared.
A class selectimp is created, which implements the interface selectcolor.
Then the method choose() of the interface selectcolor is implemented using the switch case statements.
Then there is a main() which creates the object of the class selectimp and call the choose() of the selectimp class with different parameters or arguments.