Interface variables are final and static by default, and methods are public and abstract

The question is why it was decided to have the variable as final, and static, and methods as public and abstract by default.

Is there any special reason to make them implicit, variables both final and static, and methods - public and abstract.

Why don't they allow a static method, but allow a static variable?

We have an interface to have the multiple inheritance function in Java and to avoid the problem with diamonds. But how does he solve the diamond problem, since he does not allow static methods.

In the following program, both interfaces have a method with the same name .. but when implementing only one that we implement ... is this how the diamond problem is solved?

interface testInt {
    int m = 0;
    void testMethod();
}

interface testInt1 {
    int m = 10;
    void testMethod();
}

public class interfaceCheck implements testInt, testInt1{
        public void testMethod() {
            System . out . println ( "m is"+ testInt.m );   
             System . out . println ( "Hi World!" );    
        }
}
+3
source share
3 answers

As I see it, the interface announces a set of capabilities that developers should have. This means “what” is more than “how”; This is more a specification than an implementation guide.

Therefore, methods that are not publicly available are not related to interfaces. Same thing with non-static data members that are more related to a particular implementation.

+5
source

How default they are is just a language specification. They designed it as it is.

, - . .

Java , . , Java , testInt testMethod() testInt1 testMethod(). , Java , 1 testMethod , .

+1

, . , .

, , , .

For these reasons, it would not make sense to allow methods not to be publicly available and variables not to be static.

However, I'm not sure that it would be nice to have different default values ​​for interfaces than for classes.

0
source

Source: https://habr.com/ru/post/1741045/


All Articles