Home >> Java >> Interface with method body

Interface with method body

Up to now we never said about using method body inside an Interface. But since java 8 It is possible to have method implementation inside Interface.


When we need to add a method in an older interface then problem arises. Problem is that all classes implementing that interface need to be changed. It is of course not feasible. So from Java 8 we can define method in the intrface which need not to be implemented in classes.


We can define method body in interface using keyword default. If we don’t use default keyword then compile time error will generate. So default methods in an interface are not mandatory to implement in the class.

We need not to use default keyword to define static methods. As all other methods in interface, default and static methods are also public but not abstract.


Lets take a look at following code.




After running this code we will get output like this;



Problem may arise when

  • A class implements two interface
  • Both of these interface contain default method with same signature

In this case the class need to implement the same method otherwise error will be generated. Error may like “Duplicate default methods named aaa with the parameter (p..) and (p..) are inherited from the types Interface1 and Interface2”.

Problem with defaults

Lets assume another Interface in above example:


Now Edit above Test Class like this


and WithMain.java

Here Output will seem like

I hope this post will be helpful. Drop questions in comment box regarding this.


About Log Raj Bhatt

Check Also


static blocks, non static blocks and constructor execution order

Hello guys. Here we’ll try to illustrate in which order do static blocks, non static …

Leave a Reply

Your email address will not be published. Required fields are marked *