Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java ReferenceJava Reference1

Java default Keyword
Usage and Examples



default Keyword in Java

The default keyword in Java serves a unique dual purpose. It appears in both switch statements as a fallback clause and in interfaces to define default methods. Understanding how and why to use default can make your code both more robust and more flexible.

1. default in Switch Statements

In a switch statement, the default clause is executed when none of the specified case values match the given expression. It works like an “else” block in an if-else ladder.

Example: Using default in switch

public class SwitchDefaultExample {
    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;
            default:
                System.out.println("Unknown Day");
        }
    }
}
Unknown Day

Explanation

Since the value 5 does not match any case in the switch block, the default clause is triggered and prints Unknown Day.

2. default Methods in Interfaces

From Java 8 onward, interfaces can include default methods. These are methods with a concrete implementation inside an interface. They allow developers to add new methods to interfaces without breaking existing code that implements them.

Why default methods?

Prior to Java 8, adding a method to an interface meant every implementing class had to define it. With default methods, you can provide a fallback implementation and keep old implementations working.

Example: default method in an interface

interface Vehicle {
    void start();

    default void fuelType() {
        System.out.println("Petrol or Diesel");
    }
}

class Car implements Vehicle {
    public void start() {
        System.out.println("Car starts with key");
    }
}

public class DefaultMethodExample {
    public static void main(String[] args) {
        Car myCar = new Car();
        myCar.start();
        myCar.fuelType();
    }
}
Car starts with key
Petrol or Diesel

Explanation

The Car class implements the Vehicle interface but does not override the fuelType() method. The default implementation defined in the interface is used.

Overriding default Methods

Although default methods have implementations, classes that implement the interface can still choose to override them.

Example: Overriding a default method

interface Vehicle {
    void start();

    default void fuelType() {
        System.out.println("Petrol or Diesel");
    }
}

class ElectricCar implements Vehicle {
    public void start() {
        System.out.println("Electric car starts silently");
    }

    public void fuelType() {
        System.out.println("Electricity");
    }
}

public class OverrideDefaultExample {
    public static void main(String[] args) {
        ElectricCar tesla = new ElectricCar();
        tesla.start();
        tesla.fuelType();
    }
}
Electric car starts silently
Electricity

Explanation

The ElectricCar class overrides the fuelType() method to provide its own implementation, which replaces the default.

Points to Remember



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M