This default method comes to scene when developers need to update existing interface without updating that interface implement classes updating.
Default methods must have to have an implementation.  And developer can update default method as they want but implemented classes can decide either update with newly added default method or not.

Go through below example.

import utils.CarUtils;
public interface CarCategorization {
public void displayManualCarList();
    public void displayAutoCarList();
    public void displayTipTronicCarList();
    default public void displayUpcomingCarList(){
        System.out.println("Hybrid Card List Started");
CarUtils carUtils = new CarUtils();
carUtils.displayGraceHybridCarList();
carUtils.displayToyotaHybridCarList();
System.out.println("Hybrid Card List End");
}
}
public class CarList implements CarCategorization {
public static void main(String[] args) {
        System.out.println("Started");
CarList c = new CarList();
c.displayAutoCarList();
c.displayManualCarList();
c.displayTipTronicCarList();
c.displayUpcomingCarList();
}
@Override
public void displayManualCarList() {
        System.out.println("Dx Wagon, Lancer");
}
@Override
public void displayAutoCarList() {
        System.out.println("AE110, Nissan sunny");
}
@Override
public void displayTipTronicCarList() {
        System.out.println("Celerio, ALTO");
}
}
package utils;
public class CarUtils {
public CarUtils() {
    }
public void displayGraceHybridCarList(){
        System.out.println("Grace,GP5");
}
public void displayToyotaHybridCarList(){
        System.out.println("Prius,Auqa");
}
}

One thought on “Default method in interface in java – part 1

Leave a Reply

WP Facebook Auto Publish Powered By : XYZScripts.com