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

If you have missed out the first part of this story i.e Default Methods in Java 8 ,you can read it here , else you can read the recap of first part here.

Recap : Interface IRoleService (i.e project manager) decided to assign extra duties to all (developer,tester ,ba etc) who are working on the project by introducing a new method extraDuties() in its interface. After this introduction of new extra duties  , TesterServiceImpl (i.e Tester of the application) rebelled and clear that he won’t perform any extra duties.When senior manager came to know about this situation , he advised interface IRoleService to introduce default methods so that no body will be forced to provide an implementation for extraDuties().

Now before going ahead let’s look at the code how default methods will be invoked and what will be the output in both the cases.

package com.lotusmedia.service.test;

import com.lotusmedia.service.IRoleService;
import com.lotusmedia.service.impl.DeveloperServiceImpl;
import com.lotusmedia.service.impl.TesterServiceImpl;
public class RoleServiceTestr {

	public static void main(String[] args) {
		IRoleService developerService = new DeveloperServiceImpl();
		IRoleService testerService = new TesterServiceImpl();

		developerService.extraDuties();
		testerService.extraDuties();
	}

}

When the main method is executed this is the output you will see on your console.

Developer is also performing extra duties.
Dummy guy is performing extra duties.

From here it is clear that even if any of the implementation classes don’t provide the implementation of a default method still they can access them.

Now , lets move ahead.
After the last conflict that happened between Interface IRoleService (manager) and TesterServiceImpl (tester) , senior manager has decided to bring one more manager for this project i.e one new interface INewRoleService is coming into the picture. Interesting thing is that he also wants that all the resources (developer,tester etc) to perform extra duties apart from there regular job , in technical terms this interface has also a method with the same name i.e extraDuties() , but as he is aware of the last conflict so he already marked this method as a default and provided a dummy implementation for that so nobody will be forced to provide an implementation for that.

package com.lotusmedia.service;

public interface INewRoleService {

	 default public void extraDuties(){
		 System.out.println("2nd Manager assigned jobs will be done here");
	 }
}

And our new Developer and Tester Implementation classes after implementing this interface.

package com.lotusmedia.service.impl;
import com.lotusmedia.service.INewRoleService;
import com.lotusmedia.service.IRoleService;

public class DeveloperServiceImpl implements IRoleService,INewRoleService {

	public void perform() {
		System.out.println("I will develop the application");
	}

	public void extraDuties() {
		System.out.println("I will also perform extra duties");
	}

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

As soon as this happened ( new interface implemented) , this tester (lucky chap) rebelled again and this time against this new manager that he can’t do so much of work ,meaning testers can’t do extra duties for both the managers(anways we all know what he was doing earlier also :) ). In technical terms at this moment your application starts breaking again by throwing the below mentioned error on your face.

java: class TesterServiceImpl inherits unrelated defaults 
for extraDuties() from types IRoleService and INewRoleService

Now project senior manager came back in the picture and this time he asked Tester (TesterServiceImpl) to perform extraDuties() of both the managers or he will be fired. (lol tester didn’t expect this :( ).
In technical terms now TesterServiceImpl has to provide its own implementation for extraDuties(), to fix the error in the application. So, after all this conversation tester decided to perform extra duties for both the managers (IRoleService and INewRoleService) , as its very difficult to get a new job in current recession time.

Here , is the new implementation for our TesterServiceImpl.

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

	public void extraDuties() {
		System.out.println("Doing extra duties for 1st manager");
		System.out.println("Doing extra duties for 2nd manager");		
	}

}

So, right now all set , our application is back to normal again and testers also started performing extra duties.
This went fine for few days , then suddenly one day senior manager observed that most of the time this tester guy (TesterServiceImpl) is doing nothing (I know what he was busy in), then he asked.

SM : Hey , what about extra duties , are you not doing those ?
Tester : Yeah, ofcourse I am doing that and why u always think that I am just enjoying here.
SM : Hold on , tell me how you are performing extra duties ?

In technical terms , we are going to see the implementation of extraDuties() that TesterServiceImpl has provided. So, here is the imlpementation.

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

	public void extraDuties() {
		IRoleService.super.extraDuties();
		INewRoleService.super.extraDuties();	
	}
}

Now when tester told how he is performing extra duties , senior manager was literally shocked and impressed. Here is the further conversation
SM : Hey , you were supposed to do perform extra duties by yourself and you are taking help from dummy guys to did it.
Tester : With due respect , I have been only told that to make sure that these extra duties should be completed , but no body gave me the instructions on how to do that (whether I shud do or I ask someone else to do that).
This time SM has no answer to tester , finally he is doing his job at the end doesn’t matter how he is doing that.

In technical terms we can invoke any of the default implementation from any of the interface in our actual implementation. In the implementation of extraDuties() , call has been made to the default implementation from both the interfaces IRoleService and INewRoleService.

So, here comes to an end for this fictitious interesting story .

Earlier I was planning to write one part to this series , but later I decided I will cover that in a separate use case or story (whatever u like to say).

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

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

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>