Java 8 news: Default Interface method example & argumentation

26 04 2014

Java 8 introduces significant changes. One of them are default methods in interfaces.

What has changed
Before Java 8, the interface must not have any method implementation – only the headers.

Since Java 8 the interface can have method implementations. These are called the default methods and does not have to be overriden in implementation classes (but can be). They provide default behaviour for implementations


Here is my basic example of the interface with one default and one normal method:

public interface DefaultMethodInterface {
	default void defaultMethod(){
		System.out.println("I'm in a default method!");

	void someOtherMethod();

its implementation Overrides only one of the interfaces’ methods. The key is that I do not override the default method (although it is possible). Its implementation remains as defined in interface definition:

public class DefaultlMethodInterfaceImpl implements DefaultMethodInterface{

	public void someOtherMethod() {
		System.out.println("This is interface's method implementation.");

After using these methods this way:

public static void main(String[] args) {
	DefaultMethodInterface demo = new DefaultlMethodInterfaceImpl();

I get the following output:

This is interface's method implementation.
I'm in a default method!

Both methods are accessible from object implementing the interface.

Why default methods were introduced?
The main reason is to help to maintain backward compatibility with clients that use the interface. Earlier, when the interface definition changed, it required also an implementation change. Now the interface provider, willing to add new method to it can provide the default implementation and allow the clients to work without the need to recompile their implementation.

So what is the difference between Interface and Abstract class now?
Before Java 8, to provide some default behaviour I used the abstract class with few methods implemented, and rest of them declared abstract. Now, when interface can also implement some methods – how does it differ from abstract class?

Does it look like multiple inheritance?
Java does not allow multiple inheritance (the class can extend only one class). The interface default method may somehow break this rule by implementing two interfaces both providing the same default method name. To avoid it, the java compiler will force the class to Override the duplicated method by displaying compile errors. This way it will solve the naming conflicts.

Is it good?
Well… It may be sometimes useful of course, but I would recommend to use it as a tool to provide interface backward compatibility and always consider whether it is not better to extend abstract class instead of implementing an interface with default method. If the problem with multiple inheritance is a blocker consider whether it does not mean some problems with the design. If you still think your design is good – then the default methods may help.

Did I help you?
I manage this blog and share my knowledge for free, sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!



One response

7 06 2014
javinpaul (@javinpaul)

Fantastic tutorial. By the way, I have also shared couple of examples on Java 8 lambda expressions, which complements your post. Your reader may find that useful.


Give Your feedback:

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: