Java 8 - Default Methods

 Before Java 8, it was virtually impossible to change an interface once published. Any change e.g. addition of a new method would have broken all clients.  Since backward compatibility is top priority for Java engineers, and it wasn't practical to break all clients, they came up with idea of default method.  

Java 8 enables us to add non-abstract method implementations to interfaces by utilizing the default keyword. This feature is also known as Extension Methods. Here is an example:

interface Car{
    int drive(int speed, int direction);
    default int wheels(){
        return 4;

Besides the abstract method drive() the interface Caralso defines the default method wheels(). Any concrete classes of Carinterface only have to implement the abstract method drive(). The default method wheels() method can be used directly.

  Car myCar = new Car(){
      public int drive(int speed, int direction){
          return x*y;

int totalWheels = myCar.wheels();//Invokes the default implementation in the interface.

No comments:

Tired of seeing that 500 Bad gateway error while deploying a Springboot application in AWS...?

By default, Spring Boot applications will listen on port 8080. Elastic Beanstalk assumes that the application will listen on port 5000. Th...