[JAVA] Default And Static Method

Reading Time: 3 minutes


  • In this article, We will learn default method and static method in an interface in java 8 new features.
  • Before Java 1.7 we can declare only abstract methods in an Interface.
  • However from Java 1.8 onwards we can write concrete methods also in an Interface.
  • In Interface concrete methods should be either default” or “static” only.

What is Default Method ?

The Default Method is a feature of Interfaces in Java 8. Methods that are defined inside the interface and tag with default are known as default methods.

  • The main advantage of default methods is without effecting implementation classes we can add new functionality to the interface.
  • One of the major reason for introducing default methods in interfaces is to enhance the Collections API in Java 8 to support lambda expressions.

Syntax of default methods:

  • Let’s understand the syntax of default methods through an example,
Now we will create a class that implements an interface.
  • Output:
  • In the following example, MyInterface is a functional interface that contains a default and an abstract method. So the concept of default method are use to define a method with default implementation. We can override default method also to provide more specific implementation for the method.

A Few Important Points for Default Methods

  • We can inherit the default method.
  • Redefine the default method (equivalent to overriding).

What is Static Method ?

From Java 8 we can write static methods also in an interface. Static methods are similar to default methods but the only difference is that you can’t override static methods.

Syntax of Static methods:

  • Now we understand the syntax of static methods through an example,
  • Now we will create a class which implements MyInterface interface.
  • Output

A Few Important Points for Static Methods

  • Static methods in Java are resolve at compile time. Method overriding is a part of Runtime Polymorphism.
  • Static methods can’t be overridden.
  • Abstract methods can’t be static.
  • static methods can’t use this or super keywords.



Author: Shivam

Written by 

Shivam is a Software Consultant at Knoldus Software. He has a sound knowledge of Java, Spring Boot. He is passionate about daily and continuous improvement.