Default Keyword in Java

Sachin Tripathi
This tutorial explains how to use default keyword in Java and explains with suitable examples.

Default Keyword

The following are the scenarios where default keyword can be used in Java :
  1. Can optionally be used in switch  statement.
  2. Can be used to declare default value in Java annotation.
  3. From Java 8 on wards ,can be used in interface to introduce methods with implementation.

default keyword in switch

Also Read : Transient Keyword in Java
  • Default keyword can be optionally used in the switch  statement, to label a block of statements to be executed if no case matches the specified value.
  • A code snippet showing you how important default keyword is to switch statement:
 
class Switching{
public static void main(String[] args) {
int day = 4;
String dayString;
switch (day) {
case 1:  dayString = "Monday";
break;
case 2:  dayString = "Tuesday";
break;
case 3:  dayString = "Wednesday";
break;
case 4:  dayString = "Thursday";
break;
case 5:  dayString = "Friday";
break;
case 6:  dayString = "Saturday";
break;
case 7:  dayString = "Sunday";
break;
default: dayString = "Invalid day";
break;
}
System.out.println(dayString);
}
}
OUTPUT
Thursday
Here is the explanation for the above code:
  1. Switching is the concrete class.
  2. day and dayString are two local variables of main method.
  3. We know that “The switch statement evaluates its expression, then executes all statements that follow the matching case label”.
  4. So it places Thursday in variable daystring,after evaluating the expression(day) which is 4.
    If day has any value except 1 to 7,then default keyword plays it role and it will place Invalid day in dayString.
  5. In the above  code using default is not optional,if you will not use default keyword, compiler will throw an error,because it worries that variable dayString might remain uninitialized if value of day is not between 1 to 7.

Why Default Methods?

First we will have to understand about the Default methods, in order to understand more about the default keyword. So let’s start by finding out why it was necessary to introduce Default methods in Java.
  1. Before Java 8 ,interfaces only used to have method signature not the implementations (body) of the method. They were considered to be purely abstract. As they only allow abstract methods (methods without body).
  2. All was working well, until we have thought of adding new functionality to the preexisting library (API),now they have to add abstract method in interface and implement that method in all the implementations of that interface.
  3. Including new functionality to the library(API) has started becoming too tedious.
  4. Then Default methods comes to the rescue, which solves all the problem and it allowed interfaces to have methods with their default implementation.
  5. You specify a method definition in interface is default by placing default keyword at the beginning of the method signature.
In short:”Default methods enable you to add new functionality to the interface of your library (API) and ensure binary compatibility with code written for older version of that interface”
public interface Pet {
 void beFriendly();
 default void beDescent(){
 System.out.println("Adding Decency");
 }
}
class Dog implements Pet{
 public void beFriendly(){
 System.out.println("Yeah I am friendly");
 }
 public static void main(String...s){
 Dog mydog =new Dog();
 mydog.beFriendly();
 mydog.beDescent();
 }
}
Output:
Yeah I am friendly
Adding  Decency
Here is the explanation for the above code:
  • Pet is an interface
  • It has beFriendly() which is an abstract method
  • It has beDescent() method which is a default method, so it can have implementation while being inside interface
  • Dog(class) is implementing Pet(interface)
  • In Dog we have to override all method of Pet
  • But no need to override the default method (beDescent()) of Pet
  • Even though we don’t need to override default methods in Dog(implementation of Pet) still we can access beDescent() through Dog’s object
I hope this tutorial helps you to understand the basic concept of using default keyword in Java.

Comments