Marker Interfaces

JAVING
Javarevisited
Published in
3 min readJun 22, 2020
An engineer marking a field

When programming in Java, it is always advisable to program to the interface and not to the realisation. Interfaces are programming constructs that allow us to hide the real implementation detail of our methods and classes from clients. To make the code safe from missuses and miss manipulation and also to provide flexibility we should always aim to expose our functionality via interfaces.

Interfaces are also a great Object Oriented Design tool. We know that in java we can inherit from one class, but we are allowed to implement as many interfaces as we want. This way we can keep the interfaces brief and meaningful while at the same time gradually we make the classes fulfil the relevant behaviour they are designed to do. Using interfaces that just have few methods are preferred from the OOD point of view because they respect an important principle called Interface Segregation Principle(see the S.O.L.I.D principles).

It’s not a nice feeling when you have to implement an interface that force you to implement methods you don’t really care about. Respecting the Interface Segregation Principle will help you build reusable and maintainable code!

So if the size of the interfaces we use have such a direct impact in the re-usability and maintenance of our code, could we have interfaces that have no methods at all?

The answer is Yes! This interfaces are called Marker Interfaces. It is unusual to see programmers using them but they do exist and they can be really useful. Probably one of the most popular marker interfaces in the Java API are Serializable, Cloneable and Remote. See it for yourself, go to the Java API documentation and search for any of them and you will read something like this:

The serialization interface has no methods or fields and serves only to identify the semantics of being serializable.

Now that we understood what is the idea behind marker interfaces, let’s have a look at a simple example that will illustrate how they could help us to mark without adding any additional behaviour.

Imagine that a minerals exploration company is using software to speed up the detection of certain materials, that they are looking for. In particular they are interested in certain types of rocks and metals. They refer to metals and rocks as as “Explorables”, since they are the main categories of materials they are after. Also sometimes during their activities they find many other materials that they know are also valuable in the market, but their facilities don’t have capacity to process them. They don’t want to discard them because they know they can still have some marginal profit from them, so what they do is forward them to an specialised associated company that will buy them from them. They refer to this other category of materials as “Marketables”.

They built an early stage triage program they call the “Materials Explorer” that is designed to perform an initial evaluation of the “Explorabables” and forward them to their respective departments for further evaluation and processing. First before we look into the program, let’s have a look at the different materials their system supports.

Metal is a “Explorable”
Rock is a “Explorable”

Rock and Metal are “Explorables”, they implement the Explorable.java interface and they must fulfil the contract.

Silver is a “Explorable” and a “Marketable”
Example of a marker interface

Since they are not interested in processing silver but they know that it is “Marketable”, when they find it, they use the marker interface to make sure the software knows how to forward it to their partners.

Example of some code that uses a marker interface

The marker interface helps the MarketExplorer to detect all those materials that can be given to an external partner/distributor and this way allowing the company earning extra money without having to process those themselves.

As we saw, marker interfaces are an interesting concept, yet not used widely which can provide some additional flexibility in certain occasions. I would be really curious to know if you have ever used them and if have they ever helped you solving a business problem. Please let me know your thoughts.

If you enjoyed this story please give us some 👏 and also please follow us.

--

--

JAVING
Javarevisited

The present continuous form of “to program in Java”.