Skip to main content
edited tags
Link
Ravi
  • 31.6k
  • 44
  • 124
  • 180
Tried to improve the grammer and formatting of the question.
Source Link
Elliott Frisch
  • 201.9k
  • 20
  • 166
  • 265

What is athe purpose of the default keyword in Java?

An interface in Java is similar to a class, but the body of an interface can include only abstract methods and finalfinal fields (constants).

Recently, I faced onesaw a question, which looks like this

interface interAnInterface {   public default void mymethodmyMethod() { System.out.println("D"); } } 

According to the interface definition, only abstract methods are allowed. Then, how didWhy does it allowedallow 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 myclassMyClass{ } 

instead of

class myclassMyClass {  } 

AnyoneCan anyone tell me the purpose of the default keyword  ? Is it only allowed inside an interface  ? How isdoes it differentdiffer from defaultdefault (no access modifier)  ?

What is a purpose of default keyword in Java

An interface in Java is similar to a class, but the body of an interface can include only abstract methods and final fields (constants).

Recently, I faced one question, which looks like this

interface inter {   public default void mymethod() { System.out.println("D"); } } 

According to interface definition, only abstract methods are allowed. Then, how did it allowed me to compile above code  ? What is 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{  } 

Anyone tell me the purpose of default keyword  ? Is it only allowed inside interface  ? How is it different from default (no access modifier)  ?

What is the purpose of the default keyword in Java?

An interface in Java is similar to a class, but the body of an interface can include only abstract methods and final fields (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)?

Source Link
Ravi
  • 31.6k
  • 44
  • 124
  • 180

What is a purpose of default keyword in Java

An interface in Java is similar to a class, but the body of an interface can include only abstract methods and final fields (constants).

Recently, I faced one question, which looks like this

interface inter { public default void mymethod() { System.out.println("D"); } } 

According to interface definition, only abstract methods are allowed. Then, how did it allowed me to compile above code ? What is 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{ } 

Anyone tell me the purpose of default keyword ? Is it only allowed inside interface ? How is it different from default (no access modifier) ?