top of page
keyurpatelit

Static Methods In Interface

What is a Static method?

A static method is a method that is affiliated with the class in which it is defined rather than with any object. Every instance of the class shares its static methods.


What are the Static methods in Interfaces?

Similar to Default Methods in Interfaces, Static Methods also have a method body (implementation). However, we can’t override them. Needless to say, static methods can’t be overridden.


How to declare a Static Method in Interface?

The Process of declaring a Static method in Interface is similar to defining a Static method in a class. In simple words, we have to use the static keyword while defining the method. For example, look at the code below.


interface A {

public static void m1(){

System.out.println("I am introduced in Java 1.8");

}

}

Why we need a Static Method in Interface?

If you want to provide some implementation in your interface and you don’t want this implementation to be overridden in the implementing class, then you can declare the method as static. Hence, we can secure an implementation by having it in static method as implementing classes can’t override it. Moreover, we define static methods inside classes to use them as general utility methods. Similarly, we can define static methods in interface to use them as general utility methods.


How to define main() method inside an Interface ?

Since we can have static methods in interface starting from Java 8, we can define the main() method inside an interface without violating any rule of java. Also, we can compile & run this interface successfully. For example, below code is valid.


interface Main {

public static void main(String[] args){

System.out.println("Define main() method inside interfaces now :)");

}

}

Can we have main() Method inside an Abstract class & Enum ?

Apart from the interface as shown above, we can have main() method in an abstract class & enum as below. For example, below codes are also valid from the previous version of java. For abstract class it is valid from Java 1.4 while for enum from Java 1.5.


public abstract class B {

public static void main(String[] args){

System.out.println("I am inside abstract class");

}

}


public enum C {

;

public static void main(String[] args){

System.out.println("I am inside enum");

}

}

How to call static method of an Interface?

Here we are talking about static methods inside Interfaces only. They will be called with the name of Interfaces only. We can’t call them with the help of implementing classes or any other way. For example, observe the below code.


interface A {

public static void m1(){

System.out.println("call me with the Interface name");

}

}


class Test implements A {

public static void main(String[] args){

A.m1(); //only way to call interface's static methods

}

}

Can we Override Interface’s static methods?

As aforementioned, Interface’s static methods can’t be overridden in implementing classes or in extending interfaces as they are not available to child classes by default. However, we can declare methods with the same signature in the implementing classes. They are valid, but we will not consider them as overridden methods. Hence, in this scenario method overriding concept is not valid. Also from the previous versions of Java, we know that static methods don’t participate in overriding.


interface A {

public static void m1(){

System.out.println("I am Interface's static method");

}

}


class Test1 implements A {

public static void m1(){

System.out.println("I am not an overridden method");

}

}


class Test2 implements A {

public void m1(){

System.out.println("I am not an overridden method");

}

}


class Test3 implements A {

private static void m1(){

System.out.println("I am not an overridden method");

}

}

For example, in the above code snippet all three implementing classes Test1, Test2 & Test3 contain a normal method m1(). We will not consider them as an overridden method, but general method only.


How will you define an Interface after introduction of default & static methods?

An interface declaration can contain abstract methods, default methods, static methods and constant definitions. The only methods that have implementations are default and static methods. A class that implements an interface must implement all the abstract methods declared in the interface.


The interface body can contain abstract methods, default methods, and static methods. An abstract method within an interface is followed by a semicolon, but no braces (since an abstract method does not contain an implementation). Default methods are defined with the default modifier, and static methods with the static keyword. Till Java 8, All abstract, default, and static methods in an interface are implicitly public, so you can omit the public modifier. All constant declarations defined in an interface are implicitly public, static, and final. Once again, you can omit these modifiers as well.


Note: Java 9 introduced private methods and private static methods in interfaces. Static methods in java 8 can not be private but from java 9 onward they can be.


Also read: Java Interfaces after Java 9


FAQ

Can a method be declared Default & Static together in an Interface?

A method inside an Interface can’t be declared default & static together. Default methods can’t be static & static methods can’t be default. For example, the method m1() in below code snippet is an invalid method.


interface A {

default static void m1(){

System.out.println("I am an invalid method"); // Invalid method

}

}


What is the benefit of using static methods in interfaces?

Static methods in interfaces offer a way to organize utility methods related to the interface. It helps in making the code more readable and maintainable. They also allow for code reuse without the need for a common base class.


Can interfaces have both static and default methods?

Yes, interfaces can have both static and default methods. Default methods provide a default implementation, while static methods are useful for utility methods.


Is it mandatory to provide an implementation for static methods in an implementing class?

No, it’s not mandatory to provide an implementation for static methods in implementing classes. We can use the default implementation provided by the interface.



8 views0 comments

Recent Posts

See All

Battle of the Backends: Java vs Node.js

Comparing Java and Node.js involves contrasting two distinct platforms commonly used in backend development. Here’s a breakdown of their...

Commentaires


bottom of page