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){casevalue1:// code blockbreak;casevalue2:// code blockbreak;default:// default code block}Funcționează cu
Default Methods in Interfaces
publicinterfaceInterfaceName{defaultvoidmethodName(){// default implementation}}Funcționează cu
Examples
Example 1: Default Case in Switch Statement
publicclassDefaultSwitchExample{publicstaticvoidmain(String[] args){
int day =5;switch(day){case1:
System.out.println("Monday");break;case2:
System.out.println("Tuesday");break;// other casesdefault:
System.out.println("Invalid day");break;}}}Funcționează cu
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
publicinterfaceMyInterface{defaultvoiddisplay(){
System.out.println("Default display method");}}publicclassDefaultMethodExampleimplementsMyInterface{publicstaticvoidmain(String[] args){
DefaultMethodExample example =newDefaultMethodExample();
example.display();// Calls the default method}}Funcționează cu
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
publicinterfaceMyInterface{defaultvoiddisplay(){
System.out.println("Default display method");}}publicclassOverrideDefaultMethodExampleimplementsMyInterface{
@Override
publicvoiddisplay(){
System.out.println("Overridden display method");}publicstaticvoidmain(String[] args){
OverrideDefaultMethodExample example =newOverrideDefaultMethodExample();
example.display();// Calls the overridden method}}Funcționează cu
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.
The
defaultkeyword 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
defaultkeyword specifies the block of code to execute if no case matches the switch expression.2. Default Methods in Interfaces
In interfaces, the
defaultkeyword 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
Default Methods in Interfaces
Examples
Example 1: Default Case in Switch Statement
In this example, the
defaultcase is executed if the value ofdaydoes not match any of the specified cases.Example 2: Default Method in Interface
Here, the interface
MyInterfacehas a default methoddisplay(). The classDefaultMethodExampleimplements this interface but does not override thedisplay()method, so the default implementation is used.Example 3: Overriding Default Method
In this example, the class
OverrideDefaultMethodExampleoverrides the defaultdisplay()method provided by the interfaceMyInterface.Tips and Best Practices
defaultcase in switch statements to handle unexpected values.