Java 8 Interface Changes – static method, default method

Java 8 interface changes incorporate static techniques and default strategies in interfaces. Preceding Java 8, we could have just strategy assertions in the interfaces. Be that as it may, from Java 8, we can have default strategies and static techniques in the interfaces.

Java 8 Interface

Designing interfaces have dependably been an extreme occupation provided that we need to include extra techniques in the interfaces, it will require change in all the actualizing classes. As interface develops old, the quantity of classes executing it may develop to a degree that it's unrealistic to expand interfaces. That is the reason when planning an application, the vast majority of the systems give a base execution class and after that we expand it and supersede strategies that are material for our application.

Let's investigate the default interface techniques and static interface strategies and the thinking of their presentation in Java 8 interface changes.

Java Interface Default Method

For making a default strategy in java interface, we have to utilize "default" catchphrase with the technique signature. For example,


bundle com.journaldev.java8.defaultmethod; 

public interface Interface1 { 

void method1(String str); 

default void log(String str){ 

System.out.println("I1 logging::"+str); 



Notice that log(String str) is the default system in the Interface1. By and by when a class will realize Interface1, it isn't obligatory to offer execution to default techniques for interface. This component will help us in extending interfaces with additional procedures, all we need is to give a default implementation.

Let's state we have another interface with following methods:


bundle com.journaldev.java8.defaultmethod; 

public interface Interface2 { 

void method2(); 

default void log(String str){ 

System.out.println("I2 logging::"+str); 



We realize that Java doesn't enable us to broaden various classes since it will result in the "Jewel Problem" where compiler can't choose which superclass technique to utilize. With the default strategies, the precious stone issue would emerge for interfaces as well. In such a case that a class is executing both Interface1 and Interface2 and doesn't actualize the normal default strategy, compiler can't choose which one to chose.

Extending various interfaces are an essential piece of Java, you will discover it in the center java classes just as in the greater part of the undertaking application and structures. So to ensure, this issue won't happen in interfaces, it's made obligatory to give usage to normal default techniques for interfaces. So if a class is executing both the above interfaces, it should give usage to log() technique generally compiler will toss gather time error.

A basic class that is executing both Interface1 and Interface2 will be:


package com.journaldev.java8.defaultmethod;
public class MyClass implements Interface1, Interface2 {

	public void method2() {

	public void method1(String str) {

	public void log(String str){
		System.out.println("MyClass logging::"+str);

Important focuses about java interface default methods:

  • Java interface default techniques will help us in broadening interfaces without having the dread of breaking usage classes.
  • Java interface default strategies has connect down the contrasts among interfaces and dynamic classes.
  • Java 8 interface default strategies will help us in dodging utility classes, for example, every one of the Collections class strategy can be given in the interfaces itself.
  • Java interface default strategies will help us in expelling base usage classes, we can give default execution and the execution classes can picked which one to override.
  • One of the significant purpose behind presenting default strategies in interfaces is to improve the Collections API in Java 8 to help lambda expressions.
  • If any class in the pecking order has a strategy with same signature, at that point default techniques end up unimportant. A default strategy can't abrogate a technique from java.lang.Object. The thinking is basic, this is on the grounds that Object is the base class for all the java classes. So regardless of whether we have Object class techniques characterized as default strategies in interfaces, it will be futile in light of the fact that Object class strategy will dependably be utilized. That is the reason to maintain a strategic distance from disarray, we can't have default strategies that are abrogating Object class methods.
  • Java interface default strategies are additionally alluded to as Defender Methods or Virtual expansion methods.
  • Java Interface Static Method

    Java interface static strategy is like default technique with the exception of that we can't supersede them in the execution classes. This component causes us in staying away from undesired outcomes incase of poor usage in execution classes. We should investigate this with a straightforward example.

    bundle com.journaldev.java8.staticmethod;
    public interface MyData {
    	default void print(String str) {
    		if (!isNull(str))
    			System.out.println("MyData Print::" + str);
    	static boolean isNull(String str) {
    		System.out.println("Interface Null Check");
    		return str == null ? true : "".equals(str) ? true : false;

    Now let’s see an implementation class that is having isNull() method with poor implementation.

    package com.journaldev.java8.staticmethod;
    public class MyDataImpl implements MyData {
    	public boolean isNull(String str) {
    		System.out.println("Impl Null Check");
    		return str == null ? true : false;
    	public static void main(String args[]){
    		MyDataImpl obj = new MyDataImpl();

    Note that isNull(String str) is a simple class method, it’s not overriding the interface method. For example, if we will add @Override annotation to the isNull() method, it will result in compiler error.

    Now when we will run the application, we get following output.

    Interface Null Check
    Impl Null Check

    If we make the interface method from static to default, we will get following output.

    Impl Null Check
    MyData Print::
    Impl Null Check

    Java interface static method is visible to interface methods only, if we remove the isNull() method from the MyDataImpl class, we won’t be able to use it for the MyDataImpl object. However like other static methods, we can use interface static methods using class name. For example, a valid statement will be:

    boolean result = MyData.isNull("abc");

    Important focuses about java interface static method:

    1. Java interface static strategy is a piece of interface, we can't utilize it for execution class objects.
    2. Java interface static strategies are useful for giving utility techniques, for instance invalid check, gathering arranging etc.
    3. Java interface static strategy causes us in giving security by not permitting execution classes to abrogate them.
    4. We can't characterize interface static strategy for Object class techniques, we will get compiler blunder as "This static strategy can't conceal the case technique from Object". This is on the grounds that it's not permitted in java, since Object is the base class for every one of the classes and we can't have one class level static strategy and another case technique with same signature.
    5. We can utilize java interface static strategies to expel utility classes, for example, Collections and move every last bit of it's static techniques to the relating interface, that would be anything but difficult to discover and use.

    Java Functional Interfaces

    Before I close the post, I might want to give a short prologue to Functional interfaces. An interface with precisely one conceptual strategy is known as Functional Interface.

    A new comment @FunctionalInterface has been acquainted with imprint an interface as Functional Interface. @FunctionalInterface explanation is an office to evade inadvertent expansion of dynamic techniques in the practical interfaces. It's discretionary yet great practice to utilize it.

    Functional interfaces are hotly anticipated and much searched out element of Java 8 since it empowers us to utilize lambda expressions to instantiate them. Another bundle java.util.function with pack of practical interfaces are added to give target types to lambda articulations and strategy references. We will investigate useful interfaces and lambda articulations later on posts.

    journaldev is optimized for learning.© journaldev .
    All Right Reserved and you agree to have read and accepted our term and condition