1. Overview

In this article, we will learn how to use of static and default method inside interface in Java 8. Default methods enable you to add new functionality to the interfaces. A static method is associated with the class in which it is defined rather than with any object. Every instance of the class shares its static methods.

2. Development environment

Java : Oracle JDK 1.8
IDE : IntelliJ

3. Default method inside Interface in Java 8

Before Java 8, interfaces can contain only abstract methods. The implementation of these interface methods is defined in a separate class.
Java 8 has introduced the concept of default methods in which we can also define a method with implementation inside the interface.

Remember following points before writing default method inside interface in Java 8:

The most important reason for introducing default methods in interfaces is to enhance the Collections API in Java 8 for supporting lambda expressions.

Default methods inside interface is also referred to as Defender Methods or Virtual extension methods.

Java 8 Default method inside interface example

interface DemoInterface
{   
   // abstract method
    public void square(int a);
    // default method
    default void print() {
        System.out.println("Default Method Executed");
    }
}
class DemoClass implements DemoInterface
{
    // implementation of square abstract method
    public void square(int a)
    {
        System.out.println(a*a);
    }
    public static void main(String args[])
    {
        DemoClass obj = new DemoClass();
        obj.square(4);
         // default method executed
         obj.print();
    }
}

Output:
 16
 Default Method  Executed

 

Default Methods Inside Interface with Multiple Inheritance in Java 8

In the below example both interfaces contain default methods with the same method declaration, the implementing class has to specify which default method is to be used or it should override the default method.

//A Java program to demonstrate multiple inheritance through default methods.
interface DemoInterface1
{
    // default method 
    default void print()
    {
        System.out.println("Default DemoInterface1");
    }
}
interface DemoInterface2
{
    // Default method 
    default void print()
    {
        System.out.println("Default DemoInterface2");
    }
}
// Implementation class  
class DemoClass implements DemoInterface1, DemoInterface2
{
    // Overriding default show method 
    public void print()
    {
        // use super keyword to call the print method of DemoInterface1 interface 
        DemoInterface1.super.print();
        // use super keyword to call the print method of DemoInterface2 interface 
        DemoInterface1.super.print();
    }
    public static void main(String args[])
    {
        DemoClass obj = new DemoClass();
        obj.print();
    }
}
Output:

   Default DemoInterface1
   Default DemoInterface2

We can not directly call default method inside interface, subclass will be able to override the method.

4. Static method inside Interface in Java 8

The interfaces can have static methods which is similar to define a static method inside of classes.
The static methods in interface in Java 8 are same as default method, so we need not have to implement or define them in the other classes. We can add that method to existing interfaces without changing the code in the implementation classes.

 Example: Static method inside interface in Java 8

// A simple Java program to explain static method
interface DemoInterface
{ 
    // abstract method 
    public void square (int a); 
    // static method 
    static void print()  { 
        System.out.println("Static Method Executed"); 
    }
} 
class DemoClass implements DemoInterface
{ 
    // Implementation of abstract method 
    public void square (int a)  { 
        System.out.println(a*a); 
    } 
    public static void main(String args[]) 
    { 
        DemoClass d = new DemoClass(); 
        d.square(4); 
         // Static method executed 
        DemoInterface.print(); 
    }
}
Output:
 16
 Static Method Executed

 

The static method inside interface can call it directly for DemoInterface.print() and subclass will not be able to override a static method. A static method doesn’t need to create a separate class for utility method.

Some important points for Static methods inside Interface

Interface in Java 8 Static method is part of an interface, we can not use or invoke it for implementation class objects.

Java 8 interface static methods are best for providing utility methods functionality, for example, check the null or negative value, collection sorting etc.

5. Conclusion

Implementing class of an interface can override default method of an interface but can’t override static method of an interface.

We can add static method inside interface to the existing interfaces without changing the code in the implementation classes because we cannot override them in the implementation classes.

6. References

oracle java 8 docs

Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No

Leave a Reply

Your email address will not be published. Required fields are marked *