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

42,317

Solution 1

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.

Solution 2

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.

Solution 3

Interfaces are defined for instances, not statics.

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

Solution 4

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?

Solution 5

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.

Share:
42,317
Sarang
Author by

Sarang

I am Sarang Dave from Gandhinagar, Gujarat (India). I have been working with Java in India's Largest IT Firm Tata Counsultaion Limited. I am wokring in Gujarat Budget Project in EJB Technology and jsp/servlet framework. I believe in knowledge sharing and would like to be enthusiastic in Stack Overflow & related sites. Thanks & Regards, Sarang Dave

Updated on July 05, 2022

Comments

  • Sarang
    Sarang almost 2 years

    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 ?

  • duffymo
    duffymo about 12 years
    I think you mean "none of them can be declared as final"
  • Avinash Kumar
    Avinash Kumar over 6 years
    MyInterface.myStaticMethod(), only declaration it can not be implemented on MyInterface. Indirectly you are saying that class and Interface are same.