Why can't interface methods be “static” and “final”?

In the Java interface, we can only have finite variables. We can also create static variables in the interface. But at the same time, we cannot create static / final methods, since the interface is intended only for static methods.

What exactly is the reason for not allowing static / final methods in the interface?

+6
source share
12 answers

The final method cannot be overridden. This does not match the purpose of the interface if you cannot really implement this method.

For the static part, see this question .

+11
source

You are mistaken.

  • All variables are implicitly publicly static and final in the interfaces.

  • Prior to Java 8, you cannot create static methods in interfaces. All methods are instance methods.

  • Since the sole purpose of the interface is to have classes that implement them, and since the methods in the interfaces cannot have any implementation, it makes no sense to make them final: they will not be implemented and cannot be overridden.

+6
source

Interfaces are defined for instances, not statistics.

final means cannot be overridden. This makes no sense to the interface.

+4
source

final means it cannot be overridden.

static means that it can only be called using the class name. Since the interface will have several implementations, how do you know which implementation to choose, since the interface cannot implement the method itself?

+3
source

Because they exist in the interface, which will be implemented by some class. What is the meaning of a method that cannot be implemented anywhere? (what will be the last)

+2
source

I have one more point to prove why interface methods cannot be static:

 interface MyInterface { static void myStaticMethod(); } 

Now let two classes implement "MyInterface"

//first grade

 class MyClass1 implements MyInterface { static void myStaticMethod(){ // some implementation } } 

//second class

 class MyClass2 implements MyInterface { static void myStaticMethod(){ // some implementation } } 

Now I create an instance as shown below:

1- MyInterface myObj1 = new MyClass1 (); 2- myObj1.myStaticMethod ();

3- MyInterface myObj2 = new MyClass2 (); 4- myObj2.myStaticMethod ();

// here, in lines 2 and 4, the call is incorrect, since myStaticMethod should be called using the class name (since myStaticMethod is defined as static), as shown below:

MyInterface.myStaticMethod (); → But in this case, how to call different implementations of myStaticMethod () with classes MyClass1 and MyClass2.

Thus, it was proved that static is not possible in declaring an interface method.

In conclusion, it is clear that this will be the opposite of redefining functionality.

+2
source

The interface is a pure abstract class. Therefore, all methods are in the abtract interface and must be implemented in child classes. Thus, by extension, none of them can be declared as final .

+1
source
 Why Interface methods cannot be "static" & "final"? 

All methods in the interface are clearly abstract and, therefore, you cannot define them as static or finite because static or finite methods cannot be abstract.

+1
source

In the context of Java 8 and the default methods, this question has a new meaning. static methods are now allowed and why final methods are still impossible is explained in this question .

+1
source

1: we cannot declare the final method because it contradicts this rule, since the final method cannot be overridden, but it is always necessary to define all the interface methods in it implemented classes.

2: we cannot declare a static method because it contradicts this rule, since a static method always needs the body of the method, but we cannot define any method inside the interface.

+1
source

Well static methods work on classes, not on instances, so weird / pointless. Having said that, for one reason or another, I wanted this in some situations, although I can’t remember the case now, it must have been a long time.

You can get around this though (rather an alternative api design), as the interfaces allow you to declare classes, something like this:

 interface MyInterface { static class Helpers { static void myStaticMethod(); //can be abstract etc as usual } } 

You can subclass this class, etc., as usual, and also make it abstract, abstract methods, etc. etc.

0
source

We cannot declare the interface method as static because the method is an instance method of the interface, and we cannot declare the final one because we must redefine the interface method in the implemented class. check this link for description; enter link description here

0
source

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


All Articles