Thursday 15 June 2023

Default Methods in Java interfaces

 Prior to Java 8, only abstract methods may be declared. The idea of default methods was first introduced in Java 8, though. Methods with a body are considered default methods. The primary goal of default methods in interfaces is to extend a type's functionality without modifying the implementing classes.

Prior to Java 8, all implementing classes would fail if a new method was added to an interface. In each class that implements the method, we would need to supply its implementation.

However, there are occasions when methods only need one implementation, thus it is not necessary to provide that implementation in every class. In such situation, we may designate that method in the interface as a default and provide its implementation there.

Syntax of default methods

Let’s understand the syntax of default methods through an example. Here, we have an interface with one abstract and one default method:

public interface vehicle {

   default void print() {
      System.out.println("I am a vehicle!");
   }
}

To demonstrate how default methods work, first I will create a Vehicle interface with default method 

public interface Vehicle {

    void cleanVehicle();


    default void startVehicle() {

        System.out.println("Vehicle is starting");

    }

}

Now, I will implement this interface in car class as follows

public class Car implements Vehicle {

    public void cleanVehicle() {
        System.out.println("Cleaning the vehicle");
    }

    public static void main(String args[]){
        Car car = new Car();
        car.cleanVehicle();
        car.startVehicle();
    }
}

After executing the output is as follows

As shown above, our class needs to implement only the abstract method. When we call the default method, the code defined in the interface is executed.

Multiple Inheritance with Default Methods in Java interfaces

The Diamond problem can be resolved using default method as follows

Example:

interface InterfaceA {

    default void printSomething() {

        System.out.println("I am inside A interface");

    }

}

public interface InterfaceB {

    default void printSomething() {

        System.out.println("I am inside B interface");

    }

}

public class Main implements InterfaceA, InterfaceB {


    @Override

    public void printSomething() {


        //Option 1 -> Provide our own implementation.

        System.out.println("I am inside Main class");


        //Option 2 -> Use existing implementation from interfaceA or interfaceB or both.

        InterfaceA.super.printSomething();

        InterfaceB.super.printSomething();

    }

    public static void main(String args[]){

         Main main = new Main();

         main.printSomething();

    }

}

The output is as follow 

0 comments :

Post a Comment

Note: only a member of this blog may post a comment.

Machine Learning

More

Advertisement

Java Tutorial

More

UGC NET CS TUTORIAL

MFCS
COA
PL-CG
DBMS
OPERATING SYSTEM
SOFTWARE ENG
DSA
TOC-CD
ARTIFICIAL INT

C Programming

More

Python Tutorial

More

Data Structures

More

computer Organization

More
Top