Java 8 default and static methods in Interface

In this article we will look into the enhancement done in Java 8 in respect to interface, Java 8 interface changes include static methods and default methods in interfaces.

Introduction

Traditionally Interfaces in java are actually contract. All method is by default abstract in nature.  A contract which says a class implementing interface must implement all abstract method.
Interfaces are actually real drivers behind abstraction and polymorphism in java.

The default methods were introduced to provide backward compatibility so that existing interfaces can use the lambda expressions without implementing the methods in the implementation class. Default methods are also known as defender methods or virtual extension methods

Till Java 7, we can have only method declarations in the interfaces. But from Java 8, we can have default methods and static methods in the interfaces.

java 8 Static and Default

Why we need default methods in java?

In a Traditional way, where an interface has one or multiple implementations, if any new methods are added to the interface, all the implementing classes will be forced to implement them too. Otherwise, the compiler will be going to complain and stop the program from compiling.

The default method provides the flexibility to add the default method in the interface without hampering the existing functionality.

The real benefit for default method in interface is for API and libraries developer. If any new method is introduced in the interface and we use default keyword with method, it’s not required to change all implemented classes as all classes will inherit them automatically.

Default keyword is used to make a method default inside interface, it’s not mandatory for an implementer to override Java 8 default methods, but it can if it wishes to.

 Java Default method in interface

To create default method in java interface, we need to use “default” keyword with the method signature. 

interface print{
      // Default method
       default void print() {
             System.out.println("Printing...");
       }
}

Any number of default methods and abstract methods can be in an interface.

interface I{
               // Default method
                default void a() {
                               System.out.println("print from method A");
                }

                default void b() {
                               System.out.println("print from method B ");
                }
          
                void c();   // Abstract methods
}

Importance of default method in java

Default methods were introduced to provide backward compatibility for old interfaces so that they can have new methods without affecting existing code.

  1. The default methods help to extend the functionality of interfaces without breaking the implementation of existing classes.
  2. By the use of default methods in interfaces, the Collections API was enhanced in Java 8 to support lambda expressions.

Default Method and Multiple inheritances?

interface A{

default void hello() {
   System.out.println("Hello from A");
 }
}

interface B{
  public default void hello() {
   System.out.println("Hello from B");
 }
}

class C implements B, A {

public void hello() {
  A.super.hello();
  // B.super.hello();
}
}

public class Example {
 public static void main(String... args) {
  new C().hello();
 }
}

Static Method in Interface Other addition to interface is static method apart form default method in java 8. Java 8 also allows us to define and implement static methods in interfaces We can’t override them in the implementation classes.We can define static methods inside the interface using the static keyword and are used to define utility methods.

Let’s see how to declare static methods in interface.

interface Printable{
        // Static method
        static void print() {
               System.out.println("Printing...");
        }
}

We can have any number of static methods in the interface. From Java 8, An interface can contain default, static, abstract and non-abstract methods.

Interface having Static Methods

In this example, we have multiple static methods, default methods, and abstract methods. static methods do not inherited into the implementation class so that we have called them using the interface name

interface Interface1{
   // Static method
   static void a() {
     System.out.println("Printing... from method A");
   }
   static void b() {
     System.out.println("Printing... from method B ");
   }
   // Default Method
   default void c() {
   System.out.println("Printing... from method C ");
  }
  // Abstract Method
  void getInfo();
}

public class Demo implements Interface1{

public void getInfo() {
   System.out.println("This is new style Java 8 Interface");
}

public static void main(String[] args){ 

Demo demo = new Demo();
  // Calling static Methods
  Interface1.a();
  Interface1.b();
  // Calling default method
  demo.c();
  // Calling Abstract Methods
  demo.getInfo();

}

Important points about java interface static method

  1. Java interface static method is part of interface, since static methods don’t belong to a particular object, we can’t use it for implementation class objects.
  2. A static method can be invoked within other static and default.
  3. You can define static default methods in the interface. The static method will be available for all instances of the class that implements the interface.
  4. Java interface static methods are good for providing utility methods, for example null check, collection sorting etc.
  5. The ways we have static methods in classes, we can specify method definition in an interface static method with the static keyword at the beginning of the method signature.
  6. All method declarations in an interface, including static methods, are implicitly public, so you can omit the public modifier.

 

3 thoughts on “Java 8 default and static methods in Interface”

Leave a Comment