What is the role of the `default` keyword in Java interfaces?

In Java, an interface typically contains only abstract methods and constants. However, I came across the following interface definition:

interface AnInterface {  
    public default void myMethod() {  
        System.out.println("D");  
    }  
}  

According to standard interface rules, only abstract methods are allowed. Why does the java default keyword allow this method to have a body?

On the other hand, when I tried:

default class MyClass {  
}  

I received an error saying that the default modifier is not allowed here.

Can someone clarify the purpose of the default keyword in Java? Is it restricted to interfaces? How does it differ from using no access modifier at all?

The default keyword allows interfaces to have methods with a body:

interface AnInterface {  
    default void myMethod() {  
        System.out.println("D");  
    }  
}

:small_blue_diamond: Why? To allow new methods in interfaces without breaking existing implementations.

:small_blue_diamond: Difference? Unlike regular interface methods, default methods are inherited by implementing classes.

It can be used in switch statements for a fallback case:

switch (x) {  
    case 1: System.out.println("One"); break;  
    default: System.out.println("Other");  
}

A class can inherit multiple default methods from different interfaces:

interface A {  
    default void show() { System.out.println("A"); }  
}  

interface B {  
    default void show() { System.out.println("B"); }  
}  

class MyClass implements A, B {  
    public void show() { B.super.show(); } // Resolve conflict  
}

When two interfaces have conflicting default methods, the implementing class must override and decide which one to use.