Skip to main content

Interface Methods and Interface variables


                                       Interface Method 

every method present inside the interface is always public and abstract whether we are declaring or not.

interface interf{
 void m1();
}

why it is public and why it is abstract?

public-: to make the method public because it is available to every implementing class.

abstract-:implementation class is responsible for providing an implementation.


Hence inside the interface, the following method declaration are equal.

void m1();
public void m1();
abstract void m1();
public abstract void m1();

As every interface method is always public and abstract we can't declare an interface method with the following modifiers.


Which of the following method declaration are allowed inside the interface.

public void m1(){}                   //invalid
private void m1();                    //invalid
protected void m1();                //invalid
static void m1();                      // invalid
abstract public void m1();       //valid
public abstract native void m1(); //invalid


                                    Interface Variable


An interface can contain variables the main purpose of interface variables is to define requirement-level constants.

Every interface variable is always public static final whether we are declaring or not.

interface interf{
   public static  final int x=10;
}

  

Hence within the interface, the following variable declaration is equal.

int x=10;
public int x=10;
static int x=10;
public static int x=10;
public final int x=10;
static final int x=10;
public static final int x=10;

As every interface variable is always public static final we can't declare with the following modifiers.


For the interface variable compulsory we should perform initialization at the time of declaration otherwise we will get compile time error.

eg-:

interface interf{
   int x;
}
compile time error-: '=' symbole expected.


Inside the interface which of the following variable declaration are allowed.

int x;                                                    //invalid
private int x=10;                                  //invalid
protected int x=10;                              //invalid
transient int x=10;                               //invalid
volatile int x=10;                                //invalid
public static int x=10;                        //valid

Inside the implementation class, we can access interface variables but we can't modify values.

eg-:
interface interf{
    int x=10;
}
class Test implements interf{
  public static void main(String... args){
      x=333;
      System.out.print(x);  // compile time error-: can't assign value to final variable x
}
}
class Test implements interf{
  public static void main(String... args){
     int  x=333;
      System.out.print(x);   // output is-: 333
}
}

Popular posts from this blog

Java

Codes With Java — Basics Codes With Java Java tutorials & fundamentals About Contact Privacy Basic Fundamentals Java source file structure Import Statement Static Import Packages Data Type Variables Final Variable Declaration and Access Modifier Inner classes applicable modifiers Static Modifier Synchronized Native Transient Volatile Interface Introduction Interface Declaration and Implementation Interface methods and variables Naming Conflicts Interface Marker interface and Ad...

Short Circuite Operators part 4

                                                             Short Circuit Operators In  Java logical operators , if the evaluation of a logical expression exits in between before complete evaluation, then it is known as  Short-circuit . A short circuit happens because the result is clear even before the complete evaluation of the expression, and the result is returned. Short circuit evaluation avoids unnecessary work and leads to efficient processing. 1-: AND(&&) 2-:OR(||) these are exactly same as bitwise operators (&,|) except the following differences. Single Short Circuit Operator(&,|) Both arguments Should be evaluated always. relatively performance is low. Applicable for both boolean and Integral types. Double Short Circuit Operator(...

Operators & Assignment part 3

                                                                                       Operators & Assignment 1-:instanceof Operators 2-:Bitwise Operators                                                                        instanceof Operators we can use instanceof operator to check whether the given object is of a particular type are not. Example-: /* list is a collection of objects. List l=new List(); l.add(Customer); l.add(Student); l.add(Test); Object o=l.get(...