interface

Mon, Mar 1, 2021 One-minute read

all fields in Interfaces is public static final type, cannot assign a value to final variable

Interfaces don’t contain non-initial fields because fields represent a specific implementation of data representation, and exposing them would break encapsulation. Thus having an interface with a field would effectively be coding to an implementation instead of an interface

public interface Operation {

   public double numberA;
   public double numberB ;

   public double getRes();
public interface Operation {
    public double getRes(double numberA, double numberB);
}

static method

interface fun {
    public static void method() {
        System.out.println("static method need method body");
    }
}

class A implements fun {}
class B implements fun {
    public static void method() {
        System.out.println("Override static method");
    }
}

public static void main(String[] args) {
    A a = new A ();
    // a.method(); // wrong

    fun.method(); 

    B b = new B();
    b.method();

}

default method

interface fun {
    default void method() {
        System.out.println("default need method body");
    }
}

class A implements fun {}

public static void main(String[] args) {
    A a = new A ();
    a.method(); 

    // fun.method();  wrong

}

interface1 interface2 interface3