How to access the fields of an interface in Java?


An interface in Java is a specification of method prototypes. Whenever you need to guide the programmer or, make a contract specifying how the methods and fields of a type should be you can define an interface. By default,

  • All the members (methods and fields) of an interface are public.

  • All the methods in an interface are public and abstract (except static and default).

  • All the fields of an interface are public, static and, final by default.

If you declare/define fields without public or, static or, final or, all the three modifiers. Java compiler places them on your behalf.

Example

In the following Java program, we are having a filed without public or, static or, final modifiers.

public interface MyInterface{
   int num =40;
   void demo();
}

If you compile this using the javac command as shown below −

c:\Examples>javac MyInterface.java

It gets compiled without errors. But, if you verify the interface after compilation using the javap command as shown below −

c:\Examples>javap MyInterface
Compiled from "MyInterface.java"
public interface MyInterface {
   public static final int num;
   public abstract void demo();
}

Accessing the fields of an interface

In general, to create an object of an interface type you need to implement it and provide implementation to all the abstract methods in it. When you do so, all the fields of the interface are inherited by the implementing class, i.e. a copy of the fields of an interface are available in the class that implements it.

Since all the fields of an interface are static by default, you can access them using the name of the interface as −

Example

 Live Demo

interface MyInterface{
   public static int num = 100;
   public void display();
}
public class InterfaceExample implements MyInterface{
   public static int num = 10000;
   public void display() {
      System.out.println("This is the implementation of the display method");
   }
   public void show() {
      System.out.println("This is the implementation of the show method");
   }
   public static void main(String args[]) {
      InterfaceExample obj = new InterfaceExample();
      System.out.println("Value of num of the interface "+MyInterface.num);
      System.out.println("Value of num of the class "+obj.num);
   }
}

Output

Value of num of the interface 100
Value of num of the class 10000

But, since the variables of an interface are final you cannot reassign values to them. If you try to do so, a compile-time error will be generated.

Example

 Live Demo

interface MyInterface{
   public static int num = 100;
   public void display();
}
public class InterfaceExample implements MyInterface{
   public static int num = 10000;
   public void display() {
      System.out.println("This is the implementation of the display method");
   }
   public void show() {
      System.out.println("This is the implementation of the show method");
   }
   public static void main(String args[]) {
      MyInterface.num = 200;
   }
}

Output

Compile time error

InterfaceExample.java:14: error: cannot assign a value to final variable num
   MyInterface.num = 200;
               ^
1 error
raja
Published on 06-Sep-2019 17:34:49
Advertisements