Default Methods in Java 8 Explained – Part 1 (A comic way)

Default methods in Java 8 or we can also call them defender methods is one of the new feature that you will see in Java 8.  I would rather say it is one of the most awaited feature should have been introduced in the earlier versions itself.Anyways its never been late so that it can’t be used , I think finally its a great introduction by Oracle Java Team.

Now as we know in Java (prior to 8) all the implementation classes (non abstract) of an interface have to agree the interface contract and under that contract they have to provide the concrete implementations of all the methods available in interface that they are implementing , if you missed in providing implementation for even a single method ,compiler will start complaining about it.

Now consider a scenario you want to have a method in your interface and you don’t want to force your implementation classes to provide its implementation , here where default method comes into the picture , by having default methods in your interface you can provide default implementation for it , so that your implementation class won’t be forced to provide the implementation for this method.

Let’s understand this with a very interesting real-time  situation : –
Consider I have a full fledge application where I have multiple classes that are implementing an interface. Here , I am presenting 2 (can be any number) different implementation of interface IRoleService.

package com.lotusmedia.service;
public interface IRoleService {
	public void perform();
}

Implementation Classes :

package com.lotusmedia.service.impl;
import com.lotusmedia.service.IRoleService;
public class DeveloperServiceImpl implements IRoleService {
	public void perform() {
		System.out.println("I will develop the application");
	}
}
package com.lotusmedia.service.impl;
import com.lotusmedia.service.IRoleService;
public class TesterServiceImpl implements IRoleService {
	public void perform() {
		System.out.println("I will test the application");
	}
}

So, till now all good, now suppose in my application I have decided that some team members (developer,tester etc) have to perform extra duties in addition to what they are already doing in perform() method. So, to do that I have added a new method extraDuties() in my interface.

package com.lotusmedia.service;
public interface IRoleService {
	public void perform();
	public void extraDuties();
}

Now as soon as I added this method my application starts breaking as now it is expecting that this new method should be implemented by all the implementation classes.So, I have to implement this new method extraDuties() in my all the implementations , but wait, here is the twist my Tester (TesterServiceImpl) said no , that he is not going to perform any extra duties , so it won’t provide any implementation for that.So, here is the deadlock Tester implementation doesn’t want to do any extra duties but IRoleService interface is forcing it to do some extra work.

package com.lotusmedia.service.impl;
import com.lotusmedia.service.IRoleService;
public class TesterServiceImpl implements IRoleService {
	public void perform() {
		System.out.println("I will test the application");
	}
}

So, here comes our senior manager i.e nobody else our default methods or defender methods , he listens arguments of both the parties the Interface and the Tester Implementation and suggested a solution. He asked interface to provide a default implementation (assign this task to some dummy person) for extraDuties() method , so that if any of your implementation (team members) don’t want to perform extra duties ,then some dummy guy will do that and if some body wants to perform extra duties they can provide their own implementation . He (senior manager) also asked interface to mark this method with some keyword (in our case it is default) so that if anybody sees it they will understand that nobody is forced to do extra duties.So, after senior manager suggestion Interface and implementation classes now look like this.

package com.lotusmedia.service;
public interface IRoleService {
	public void perform();
	default public void extraDuties(){
		System.out.println("Dummy guy is performing extra duties.");
	}
}
package com.lotusmedia.service.impl;
import com.lotusmedia.service.IRoleService;
public class DeveloperServiceImpl implements IRoleService {
	public void perform() {
		System.out.println("I will develop the application");
	}
	public void extraDuties() {
		System.out.println("Developer is also performing extra duties");
	}
}

So, here if you see Developer Implementation is providing its own implementation of extraDuties() while on the other side Tester Implementation ignored the extraDuties() and didn’t provide any implementation.Tester will enjoy the default implementation provided by interface , that some virtual dummy guy is performing extra duties for tester. So, that’s why virtual methods in Java 8 also known as Virtual Extension methods.

So, from here definition of default methods is clear : –

Default methods provide java developers an ability to add new methods to an 
existing interface without breaking the overall functionality of the application.

Till now here is only the one side of the story , in the next part  we will see that how all the implementations(Developer,Tester etc) come together and protested against default methods.

Story Continued ….
Stay Tuned

Disclaimer : All the characters in the above story are fictitious and they have no resemblance to any developer , tester , manager or senior manager.

Let'sConnect

Saurabh Jain

A Developer working on Enterprise applications ,Distributed Systems, Hadoop and BigData.This blog is about my experience working mostly on Java technologies ,NoSQL ,git , maven and Hadoop ecosystem.
Let'sConnect

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

2 thoughts on “Default Methods in Java 8 Explained – Part 1 (A comic way)

  1. The example look promising for explaining the default method mechanism. However, i would like to add here is that many of the usecases that were applying for abstract classes are now debatable with introduction of default method feature.

Add Comment Register



Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>