0

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?

Community
  • 1
  • 1
arjacsoh
  • 8,932
  • 28
  • 106
  • 166
  • 1
    "a field" is not a method. It a variable. The fields defined in the interface are static and final. – km1 Aug 31 '12 at 13:40
  • In the context of an interface, a field would be a constant, not technically a _variable_. – adarshr Aug 31 '12 at 14:33

4 Answers4

7

"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 { }
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

"a field in an interface is..."

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

adarshr
  • 61,315
  • 23
  • 138
  • 167
  • Ok, my misunderstanding comes then from my the fact, that I was unaware that an interface can contain variables. I thought, that only methods were admissible. I cannot remember having encountered an interface with variables. That' s all. – arjacsoh Aug 31 '12 at 13:52
  • @arjacsoh an interface can only contain constants, not variables. – adarshr Aug 31 '12 at 14:32
0

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.

hvgotcodes
  • 118,147
  • 33
  • 203
  • 236
0

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();
    }
}
km1
  • 2,383
  • 1
  • 22
  • 27