An interface in Java is similar to a class, but the body of an interface can include only abstract methods and
finalfields (constants).
Recently, I saw a question, which looks like this
interface AnInterface { public default void myMethod() { System.out.println("D"); } } According to the interface definition, only abstract methods are allowed. Why does it allow me to compile the above code? What is the default keyword?
On the other hand, when I was trying to write below code, then it says modifier default not allowed here
default class MyClass{ } instead of
class MyClass { } Can anyone tell me the purpose of the default keyword? Is it only allowed inside an interface? How does it differ from default (no access modifier)?