Skip to main content
Documents
Share
LinkedIn
Facebook
Twitter
Copy
Java keywords

default Keyword in Java

The default keyword in Java has multiple uses, primarily in switch statements and interface methods. It provides a default case in switch statements and allows methods in interfaces to have a default implementation.

Usage

1. Default Case in Switch Statements

In a switch statement, the default keyword specifies the block of code to execute if no case matches the switch expression.

2. Default Methods in Interfaces

In interfaces, the default keyword allows the creation of methods with a default implementation. This feature, introduced in Java 8, enables interfaces to evolve without breaking existing implementations.

Syntax

Switch Statement

switch (expression) {
    case value1:
        // code block
        break;
    case value2:
        // code block
        break;
    default:
        // default code block
}

Default Methods in Interfaces

public interface InterfaceName {
    default void methodName() {
        // default implementation
    }
}

Examples

Example 1: Default Case in Switch Statement

public class DefaultSwitchExample {
    public static void main(String[] args) {
        int day = 5;
        switch (day) {
            case 1:
                System.out.println("Monday");
                break;
            case 2:
                System.out.println("Tuesday");
                break;
            // other cases
            default:
                System.out.println("Invalid day");
                break;
        }
    }
}

In this example, the default case is executed if the value of day does not match any of the specified cases.

Example 2: Default Method in Interface

public interface MyInterface {
    default void display() {
        System.out.println("Default display method");
    }
}

public class DefaultMethodExample implements MyInterface {
    public static void main(String[] args) {
        DefaultMethodExample example = new DefaultMethodExample();
        example.display(); // Calls the default method
    }
}

Here, the interface MyInterface has a default method display(). The class DefaultMethodExample implements this interface but does not override the display() method, so the default implementation is used.

Example 3: Overriding Default Method

public interface MyInterface {
    default void display() {
        System.out.println("Default display method");
    }
}

public class OverrideDefaultMethodExample implements MyInterface {
    @Override
    public void display() {
        System.out.println("Overridden display method");
    }

    public static void main(String[] args) {
        OverrideDefaultMethodExample example = new OverrideDefaultMethodExample();
        example.display(); // Calls the overridden method
    }
}

In this example, the class OverrideDefaultMethodExample overrides the default display() method provided by the interface MyInterface.

Tips and Best Practices

  • Switch Statements: Always include a default case in switch statements to handle unexpected values.
  • Interface Evolution: Use default methods in interfaces to add new methods without breaking existing implementations.
  • Handling Multiple Inheritance: When multiple interfaces have default methods with the same name, the implementing class must resolve the ambiguity by overriding the method.
  • Override with Caution: When overriding default methods, ensure that the new implementation does not break the existing contract of the interface.
  • Documentation: Clearly document the behavior of default methods to avoid confusion for users of the interface.