Java default Keyword

The default keyword in Java is used in two contexts: within a switch statement to specify the default block of code to execute when no case matches, and in interface declarations to define default methods. This guide will cover both usages.

Table of Contents

  1. Introduction
  2. default in switch Statements
    • Syntax
    • Examples
  3. default in Interface Methods
    • Syntax
    • Examples
  4. Real-World Use Case
  5. Conclusion

Introduction

The default keyword serves different purposes based on the context in which it is used. In switch statements, it provides a way to execute a block of code when none of the case labels match the switch expression. In interfaces, it allows defining methods with a default implementation that classes implementing the interface can inherit or override.

default in switch Statements

Syntax

The syntax for using the default keyword in a switch statement is as follows:

switch (expression) { case value1: // code to be executed if expression equals value1 break; case value2: // code to be executed if expression equals value2 break; // you can have any number of case statements default: // code to be executed if expression doesn't match any case break; } 

Example

To demonstrate the usage of the default keyword in a switch statement, we will print the name of the day based on the value of the variable day.

public class DefaultSwitchExample { public static void main(String[] args) { int day = 8; switch (day) { case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; case 3: System.out.println("Wednesday"); break; case 4: System.out.println("Thursday"); break; case 5: System.out.println("Friday"); break; case 6: System.out.println("Saturday"); break; case 7: System.out.println("Sunday"); break; default: System.out.println("Invalid day"); break; } } } 

Output:

Invalid day 

default in Interface Methods

Syntax

The syntax for defining a default method in an interface is as follows:

public interface InterfaceName { // abstract method void abstractMethod(); // default method default void defaultMethod() { // default implementation } } 

Example

To demonstrate the usage of the default keyword in interface methods, we will create an interface with a default method and a class that implements this interface.

public interface Greeting { void sayHello(); default void sayGoodbye() { System.out.println("Goodbye!"); } } public class GreetingImpl implements Greeting { public void sayHello() { System.out.println("Hello!"); } } public class DefaultMethodExample { public static void main(String[] args) { GreetingImpl greeting = new GreetingImpl(); greeting.sayHello(); greeting.sayGoodbye(); } } 

Output:

Compilation failed. 

Real-World Use Case

Providing Default Behavior in Interfaces

In real-world applications, the default keyword in interfaces can be used to provide default behavior for methods. This allows adding new methods to interfaces without breaking existing implementations.

Example

public interface Calculator { int add(int a, int b); default int subtract(int a, int b) { return a - b; } } public class SimpleCalculator implements Calculator { public int add(int a, int b) { return a + b; } } public class DefaultMethodRealWorldExample { public static void main(String[] args) { SimpleCalculator calculator = new SimpleCalculator(); System.out.println("Addition: " + calculator.add(10, 5)); System.out.println("Subtraction: " + calculator.subtract(10, 5)); } } 

Output:

Compilation failed. 

Conclusion

The default keyword in Java serves important roles in both switch statements and interface declarations. In switch statements, it allows for a default case when no other cases match. In interfaces, it enables the definition of default methods, providing flexibility and backward compatibility. By understanding and using the default keyword, you can write more robust and flexible Java programs.

Leave a Comment

Scroll to Top