The variables(int blue, int yellow, int pink) of an interface are always declared as final. Final variables are those variables, whose values are constants and can’t be modified(changed). The class that implements the interface will use the variables as declared within the interface and can’t modify( changed) the value of the variable.
How to define and access Interface Variables and Methods in Java(Java Interface Example)
interface selectcolorr
{
//Variables declaration
int blue=4;
int yellow=5;
int pink=6;
public void chooseee(int color); //function with argument
}
class selectcol implements selectcolorr //class
{
public void chooseee(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]);
selectcol st=new selectcol();
st.chooseee(a1);
st.chooseee(b1);
st.chooseee(c1);
}
}
Explanation…
In this example first, an interface selectcolorr is created and the value for the integers blue, yellow, and pink are set as 4,5,6(int blue=4, int yellow=5, int pink=6).
Then a method choosee() which takes an integer parameter is declared.
A class selectcol is created, which implements the interface selectcolor.
Then the method choosee() of the interface selectcolorr is implemented using the switch case statements.
Then there is a main() which creates the object of the class selectcol and calls the choosee() of the selectcol class with different parameters or arguments.