What is Default Keyword in Java

Introduction 

In this article, we will learn about the default keyword in Java. The default keyword used in switch statements is a fallback option. When a switch statement is evaluated, and none of the cases match the input value, the default case is executed.

What is the default class in Java?

The default class is created when you do not explicitly specify a class modifier like public, protected, or private in your class declaration.

The default class access level is package private, meaning the class can be accessed only within the same package in which it is declared. If no access level modifier is used, the class is considered to have the default access level. 

class DemoClass  {
  //class code goes here
}

In this example, the DemoClass is declared with the default access level, which means that it can be accessed only within the same package where it is defined. 

Is default a keyword in Java?

Yes, default is a reserved keyword in Java. It is used in several contexts, such as defining default values for variables, specifying default access modifiers for classes, interfaces, and their members, and as a keyword in switch statements.

In this statement, the switch statements default is used as a case label to provide a default action to be executed when none of the other cases match the input value. The default case is optional and appears at the switch statement's end.  

 public class Main {
  public static void main(String[] args) {
    int week = 4;
    switch (weekend) {
      case 6:
        System.out.println("Saturday");
        break;
      case 7:
        System.out.println("Sunday");
        break;
      default:
        System.out.println("Looking forward to the Weekend");
    }    
  }
}

Output 

  

What is static and default keyword in Java?  

1. Static keyword that can define a static method or variable in Java. A static method or variable belongs to a class rather than an instance of the class, meaning it can be accessed without creating an instance. For example, if you have a Math class and want to use the PI value without creating an instance of the Math class, you can use Math.PI since PI is a static variable.

Here's an example of a static method 

//Java code online
  public class Demo {
   public static void printMessage() {
      System.out.println("Hello!");
   }

   public static void main(String[] args) {
      printMessage();
   }
}

In this program, the printMessage() method is static, meaning that it belongs to the Demo class and can be called directly without creating an instance.

Output 

 

2. Default keyword is used in the context of interfaces in Java. It defines a default implementation of a method in an interface. This allows the interface to provide a default implementation for a method, which can be overridden by implementing classes if needed.

Here's an example of a default method 

public interface demo  {
   default void printMessage() {
      System.out.println("Hello!");
   }
}

public class ExampleImpl implements Demo {
   public static void main(String[] args) {
      ExampleImpl obj = new ExampleImpl();
      obj.printMessage();
   }
}

In this statement, the interface defines a default method called printMessage(). The ExampleImpl class implements the Example interface and inherits the default implementation of printMessage(). The printMessage() method is called on an instance of the ExampleImpl class in the main() method, which prints Hello. If the ExampleImpl class overrides the printMessage() method, it will use the overridden implementation instead of the default implementation provided by the interface. 

Conclusion

In this article, you will learn about the code that taught us the default keyword in Java. 

FAQs 

Q- What is the default keyword in Java?

A- The default keyword in Java is used in switch statements as a fallback case when none of the other cases match the expression being evaluated. It is also used in interface methods to provide a default implementation that can be overridden by implementing classes.

Q- How is the default keyword used in switch statements in Java?

A- The default keyword is used in switch statements to specify a default case executed when none of the other cases match the expression being evaluated. The default case is optional, but it is a good practice to include it to handle unexpected input.

Q- How is the default keyword used in interface methods in Java?

A- The default keyword is used in interface methods to provide a default implementation that can be overridden by implementing classes. This allows interfaces to evolve without breaking existing implementations. The default method provides a default implementation if the implementing class does not provide its implementation.

Q- Can a Java interface have a default constructor?

A- No, Java interfaces cannot have constructors, including default constructors. This is because an interface is purely abstract, and all its methods are implicitly abstract without implementation. Therefore, there is no need for constructors in interfaces.

1. In a switch statement, default is used to specify the default case that should be executed if none of the other cases match the value being switched on. 

2. In an interface, default is used to specify a default implementation for a method. This is useful when adding new methods to an existing interface without breaking existing implementations  


Similar Articles