Java Keywords (Part XVII): The default keyword

This article summarize the use of the keyword default. I suggest you review Java Keywords (Part V): Classes vs Interfaces and Java Keywords (Part IX): Switch Statements before proceeding.

Java keyword list

abstract continue for new switch
assert default goto* package synchronized
boolean do if private this
break double implements protected throw
byte else import public throws
case enum instanceof return transient
catch extends int short try
char final interface static void
class finally long strictfp volatile
const* float native super while
Keyword marked with an asterisk (*) are keywords that, although valid, are not used by programmers.
Up until Java 8 (released in March, 2014), the default keyword had a single purpose, which was to indicate the default case in a switch statement. To illustrate quickly

String color = "red";
switch(color) {
    case "red":
    case "white":
	case "blue":
    	System.out.println(color + " is a supported color.");
        break;
	default:
    	System.err.println(color + " is not supported.");
        break;
}
Basically, in this context, the default keyword is akin to the else block in an if/else flow-control structure. Please understand that the code inside the default case does not have to handle exceptional cases, just like the else block. It is simply to tell the program "do these things for all other cases" whatever "these things" are. It could be error handling.

The second use of the default keyword for default methods in interfaces; introduced in Java 8. At the time I wrote that article, I wanted to cover this topic. After much deliveration, I decided to wait and write another article (this one) where I could wrap up the use and cover both cases. The reason was to keep it short. Covering both cases would have made the article too long and probably too "all over the place."

Here is an example of a default method in a Java interface:


public interface Pet {
	void learnTrick(String trickName);
	void doTrick(String trickName);
	void giveName(String name);
    void respondByName(String trickName);
    void dressUp();
    
    default void identify() {
    	System.out.println("I am simply a pet.")
    }
}
You may ask, what is so special about this? Well, up until Java 8, interfaces could not contain concrete methods. Concrete methods are methods with a body. In contrast, as you might recall, an abstract method does not have a method body and classes implementing an interface must implement all of its methods. According to Oracle
Default methods enable you to add new functionality to the interfaces of your libraries and ensure binary compatibility with code written for older versions of those interfaces.
You may ask, what does this mean exactly?
  • It means that code written for Java 7 or earlier will continue to work if adopting classes implemeting interfaces written for Java 8 containing default methods.
  • It means that code that was written for any Java version will not break if an existing interface is updated to include default methods.
    • Keep in mind is that a default method can be overridden by an implementing class, but it does not have to.
Another added benefit of using default methods is that implementing classes no longer need a common concrete parent implementing default behavior; thus simplifying class hierarchy. Since Java supports single inheritance of classes, this is a huge added benefit. For more on the topic of default methods, please visit the Default Methods Java tutorial article on Oracle Java website.

Comments

Popular posts from this blog

Combining State and Singleton Patterns to Create a State-Machine

The Beauty of the Null Object Pattern

Exception Handling: File CRUD Operations Example