Java Programming to Interfaces using Static Methods

Filed Under: Java

In this quick tutorial, we would understand how static methods on interfaces helps us design better code and support the basic design principle: “Program to an interface, not an implementation”. You can find basic details of static methods at java 8 interface changes. Let’s get started with programming to interfaces design principle.

Java Programming to Interfaces

java programming to interfaces, java interface programming
This has been one of the most basic design principles. We should always use interfaces to effectively enforce development contracts while maintaining loose coupling of code.

In Java we have seen several Collection classes follow these principles, where the concrete implementation only exposes the methods they implement from the interfaces, leaving the rest of the implementation in the private methods.

Problem with programming to interface implementation

Let’s introduce an interface SuperHeros which gives us list of characters.


public interface SuperHeros {
	public List<String> getCharacters();
}

There are many comic studios and each studio would have their own concrete implementation.


public class DCSuperHeros implements SuperHeros {
	List<String> characters = List.of("Super Man", "Bat Man", "Wonder Woman", "Cyborg", "Flash");

	public List<String> getCharacters() {
		return characters;
	}
}

public class MarvelSuperHeros implements SuperHeros {
	List<String> characters = List.of("Spider Man", "Captain America", "Daredevil", "Wolverine", "Iron Man");

	public List<String> getCharacters() {
		return characters;
	}
}

The customers/client would use the code as below to write program against interfaces.


SuperHeros marvelHeros = new MarvelSuperHeros();
SuperHeros dcHeros = new DCSuperHeros();

What this does is that it inherently ties down the client code with concrete implementation and then it becomes difficult to change the concrete implementations later as this has already been exposed to the clients.

Even though we have interfaces, but we have provided our concrete implementation details to the customer and broken the design principle: Program to an interface, not an implementation. So how do we fix it and make it better?

Java 8 static methods on interfaces

Java 8 brought in a lot of new features and one of them is static method on interfaces. This very interesting and important feature is mostly overlooked by the developer community.

In order to follow our design principle we would introduce static methods which are static factories to create an instance of the Concrete Implementation of the Superheroes. Let’s add the static method to the interfaces:


public interface SuperHeros {
	public List<String> getCharacters();

	static SuperHeros createDCSuperHeros() {
		return new DCSuperHeros();
	}

	static SuperHeros createMarvelSuperHeros() {
		return new MarvelSuperHeros();
	}
}

Additionally make your concrete implementations non public. Now the customers/client would use the code as:


SuperHeros marvelHeros = SuperHeros.createMarvelSuperHeros();
SuperHeros dcHeros = SuperHeros.createDCSuperHeros();

Now with help of static methods we were able to write static factories for our SuperHeros implementations.

Limitation: This approach can be applied only when you have control over the interface, if it’s a third party interface then it won’t work and you are better off with the general approach.

Summary

In this quick tutorial we learnt how to program to interfaces and how a static method in interfaces helps us achieve this.

And to wrap up, you’ll find the source code to this article on Github.

Comments

  1. Thakib Kayodé Adéchinan Salami says:

    Awesome

  2. Ketan says:

    Thank you so much.

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages