Accessing Interface in Java
Java Programming

Accessing Interface in Java

 

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);

}

}

Java Interface Example

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.

Recommended Posts

C++ Programming

Visibility modes in C++

In C++, visibility modes refer to the accessibility of class members (such as variables and functions) from different parts of a program. C++ provides three visibility modes: public, private, and protected. These modes control the access levels of class members concerning the outside world and derived classes. Public: Members declared as public are accessible from […]

Rekha Setia 
C++ Programming

Inheritance in C++

In C++, inheritance is a fundamental concept of object-oriented programming (OOP) that allows you to create a new class based on an existing class, known as the base or parent class. The new class is called the derived or child class. Inheritance facilitates code reuse and supports the creation of a hierarchy of classes. There […]

Rekha Setia 
C++ Programming

C++ Classes and Objects

In C++, classes and objects are fundamental concepts that support object-oriented programming (OOP). Here’s a brief overview of classes and objects in C++: Classes: In C++, a class is a user-defined data type that allows you to encapsulate data members and member functions into a single unit. Classes are the building blocks of object-oriented programming […]

Rekha Setia 

Leave A Comment