Default method in interface in java – part 1

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");
}
}

Advertisements

One comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s