简体   繁体   中英

Why Interface methods cannot be "static" & "final"?

In Java Interface, we can have only final variables possible. We can also create static variables in Interface. But, at the same time we are not able to create static/final methods as Interface are only meant for Static Methods.

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

A final method can't be overridden. That defies the purpose of having an interface if you cannot actually implement the method.

For the static part, see this question .

You got it wrong.

  1. All variables are implicitly public static and final in interfaces.

  2. Prior to Java 8, you can't create static methods in interfaces. All methods are instance methods.

  3. Since the only goal of an interface is to have classes implementing them, and since methods in interfaces can't have any implementation, making them final would make no sense: they would have no implementation, and could not be overridden.

Interfaces are defined for instances, not statics.

"final" means "can't be overridden". That makes no sense for an interface whatsoever.

final means that it cannot be overriden.

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

Because they are there in an interface to be implemented by some class. What would be the point of a method that can not have an implementation anywhere? (which is what final would suggest)

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

interface MyInterface {
        static void myStaticMethod();    
}

Now let's have two classes are implementing "MyInterface"

// first class

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

// second class

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

Now I am instantiating like below:

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

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

// here at line 2 & 4 , it's wrong calling as myStaticMethod should be called using class name(because myStaticMethod is defined as static) like below:

MyInterface.myStaticMethod();--> But in this case,how to call different implementations of myStaticMethod() by MyClass1 & MyClass2 classes.

So it's proved that static can not be possible in interface method declaration.

For final ,it's quite clear that it will opposite to override functionality.

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

Why Interface methods cannot be “static” & “final”?

接口中的所有方法都是显式抽象的,因此您不能将它们定义为 static 或 final,因为 static 或 final 方法不能是抽象的。

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

1: we can't declare a final method ,because it contradicts it's rule as final method can't be override,but always need to define all the interface methods in it implemented classes.

2: we can't declare a static method ,because it contradicts it's rule as static method always needs the method body but we cant define any method inside a interface.

Well static methods work on classes and not instances so kind of strange/pointless. Having said that I've for one reason or another wanted this in some situations, though can't remember a case now so must have been long ago.

You can "work around" this though (rather alternative api design) as interfaces allow you to declare classes, so something like this:

interface MyInterface {

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

You can subclass that class etc as normal of course, as well make it abstract, abstract methods etc etc.

We can not declare method of interface as static because method of interface instance method and we can not declare final because it is necessory to override method of interface in implemented class. for description check this link enter link description here

By default all the methods present inside an interface are public and abstract. If you declair a method as final inside an interface 1st of all you will get a compiler error and not even then it doesn't make any sense to have a final method because you will never be in a position to override it in child class.

In case of static even if Java allow in what so ever version it's not a good programming practice to use static inside an interface because for static methods u must have to provide the implementation which you must not provide inside an interface. Moreover, even if you provide the implementation inside an interface still u have to override it and then have to call it by the class name.

Interface cant have static method because if you know the static property that method declared static can be called without creating any object of class and sttaic methods are part of class not instance of class, so the answer is that how can you call abstract method till java 7, In java 8 you can declare method as static and call it by interface name dot method name.

Now answer fo final is that , final method is not overriden so how you will override it when class will inherit it

why can't we make Interface methods final?

because if you make a method final then you can not override it and the sole purpose of Interface is to have methods that will be overridden by all those class that implements that Interface.

why can't we make Interface methods static?

In Java 8 it's possible, you can make methods static but that method should have a method body

interface Test{
    static void hello(){
       System.out.println(“hello world”);
    }
}

and you can access this method from a class implementing this Interface by

Test.hello();

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