Pages

Friday, December 31, 2021

Java 8 Default and Static Methods - Interface New Concepts

1. Overview


In this Java 8 new concept, We are going to learn today what is the Default Method in Java 8 and why it is introduced in java 8 changes.

Java 8 introduces a new concept of default method implementation in interfaces. This capability is added for backward compatibility so that old interfaces can be used to leverage the lambda expression capability of Java 8. For example, the List interface is not having the forEach() method. If they add this method as an abstract method then all List implementation classes must have to provide the implementation since it is common functionality to iterate the list. So they had to introduce this concept to enhance features and support for the backward classes.

Java 8 Default and Static Methods




2. Java 8 Default Method Syntax


Just need to declare a method keyword default.

Interface InterfaceName {
 
    default void doSomeThing(){
        // some logic here to process
    }
}

3. Java 8 Default Method Example Program


interface TwoWheeler {

    default String print() {

        return "TwoWheeler";
    }
}

class HondaActiva implements TwoWheeler {

    public void printName() {
        System.out.println("This is Honda Activa 7G and it is a " + print() + " Vehicle");
    }

}


Main method:

package com.javaprogramto.w3schools.programs.java8;

/**
 * 
 * Default Methods examples.
 * 
 * @author JavaProgramTo.com
 *
 */
public class DefaultMethods {

    public static void main(String[] args) {

        HondaActiva activa = new HondaActiva();
        activa.printName();

    }

}

Output:

This is Honda Activa 7G and it is a TwoWheeler Vehicle

Note: The default method can not be called with the Interface name. It must be accessed in its implementation classes.

4. Java 8 Multiple default methods


Multiple default methods are allowed in a single interface. Defined two interfaces with the same default method. Refer to the below code.

interface TwoWheeler {

    default String printWheelsType() {

        return "TwoWheeler";
    }
}

interface FourWheller {
    default String printWheelsType() {

        return "FourWheeler";
    }

}

Now, these two interfaces are implemented by a class as below and it throws a compile-time error saying "Duplicate default methods named printWheelsType with the parameters () and () are inherited from the types FourWheller and TwoWheeler".

This error can be cleared with two solutions.

Solution 1: 

Override printWheelsType() method in this class and provide its own logic.

class WheelsUtility implements TwoWheeler, FourWheller {

    @Override
    public String printWheelsType() {
        return "Utility for Wheels";
    }

}

Solution 2:

Override and call a specific to your implementation.


class WheelsUtility implements TwoWheeler, FourWheller {

    @Override
    public String printWheelsType() {
        // TODO Auto-generated method stub
        return FourWheller.super.printWheelsType();
    }

}

The above code used the super keyword to call the default method from FourWheeler interface.

5. Java 8 Static Default Method


Shall use the static keyword on the default method?

yes. It is allowed to use if you have any utility methods that can be called directly with the interface name.


interface DateInterface {

    default void getLocale() {

    }

    static Date getTodayDate() {

        return new Date(Calendar.getInstance().getTimeInMillis());

    }

}

Static default method invocation:

Date date = DateInterface.getTodayDate();

6. Conclusion


In this quick article, we have seen the new concept of Java 8 Default Methods. And also how to solve if the implemented two interfaces have the same default methods.

At last shown program on static default method in the interface.

No comments:

Post a Comment

Please do not add any spam links in the comments section.