简体   繁体   中英

Meaning of static and final fields of an interface

Stimulated by the question Interface vs Abstract Classes and the accepted answer, I would like to have a more detailed and clarified answer. In particular I cannot understand the statement "a field in an interface is implicitly static and final". Does it mean that a class A implementing an interface containing method foo() can invoke the method as A.foo() ?

What concerning final: As long as interfaces contain only methods, given an abstract class A which implements an interface with a method foo() and an ordinary class B which extends the abstract class A , cannot the class B override the foo method? As far as I am concerned, final methods are impossible to be overridden. What is true finally?

"a field in an interface is implicitly static and final".

In an interface writing

int N = 1;
public int N = 1;
static int N = 1;
public static int N = 1;
// also
final int N = 1;
public final int N = 1;
static final int N = 1;
public static final int N = 1;

are all the same.

Does it mean that a class A implementing an interface containing method foo() can invoke the method as A.foo()

Fields and method are both members, but methods and fields are not the same thing.

Methods in an interface cannot be static or final , but are implicitly public and abstract

int foo();
public int foo();
abstract int foo();
public abstract int foo();

are all the same for an interface.

As far as I am concerned, final methods are impossible to be overridden

final instances methods cannot be overridden and final static methods cannot be hidden.

Similarly nested interfaces, classes and annotations are public and static. Nested interfaces and annotations are also implicitly abstract.

interface A {
    public static class C { }
    public static /* final */ enum E {; }
    public static abstract interface I { }
    public static abstract @interface A { }
}

"a field in an interface is..."

It is talking about fields . A field is not a method.

Does it mean that a class A implementing an interface containing method foo() can invoke the method as A.foo()?

No, you need to create an instance of A with new and then implement foo on the instance.

 As long as interfaces contain only methods, given an abstract class A which implements an interface with a method foo() and an ordinary class B which extends the abstract class A, cannot the class B override the foo method? As far as I am concerned, final methods are impossible to be overridden. What is true finally?

interface methods cannot be final, so the question makes no sense. A subclass of an abstract class that implements an interface can provide their own implementations for the interface methods.

Try looking at this.

public interface A {
    int x = 4;
    public void printVal();
}

public class B implements A {

    public void printVal() {
        System.out.println(A.x);
    }

    public static void main(String [] args) {
        System.out.println(A.x);

        (new B()).printVal();
    }
}
Interface can only contain abstract methods, properties but we don’t
need to put abstract and public keyword. All the methods and properties
defined in Interface are by default public and abstract.

Every field in an interface is public, static and final because...

Interface variables are static because Java interfaces cannot be instantiated 
in their own right; the value of the variable must be assigned in a static 
context in which no instance exists. The final modifier ensures the value
assigned to the interface variable is a true constant that cannot be 
re-assigned by program code.

Ex:

public interface Test
  {
    int value = 3; //it should be same as public static final int value = 3;
  }

In case of Member function of interface .

A method declaration within an interface is followed by a semicolon,
but no braces, because an interface does not provide implementations
for the methods declared within it. All methods declared in an interface 
are implicitly public, so the public modifier can be omitted.

Means Methods are not Final in Interface.

For more details go through this Tutorial

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM