⬅ Previous Topic
Java continue KeywordNext Topic ⮕
Java do Keyword⬅ Previous Topic
Java continue KeywordNext Topic ⮕
Java do Keyworddefault
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.
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.
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
Since the value 5
does not match any case
in the switch
block, the default
clause is triggered and prints Unknown Day
.
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.
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.
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
The Car
class implements the Vehicle
interface but does not override the fuelType()
method. The default implementation defined in the interface is used.
Although default methods have implementations, classes that implement the interface can still choose to override them.
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
The ElectricCar
class overrides the fuelType()
method to provide its own implementation, which replaces the default.
default
keyword in a switch
acts as a fallback when no case
matches.default
keyword in an interface
provides a base implementation for a method from Java 8 onward.⬅ Previous Topic
Java continue KeywordNext Topic ⮕
Java do KeywordYou 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.