Marker Interface example in Java

Introduction

The marker interface pattern is a design pattern that provide run-time type information about objects. It provides a means to associate metadata with a class when there is no explicit support for such metadata.

The marker interface in Java does not have any method inside it, in other words, a marker interface in Java is an empty interface.

Marker interfaces in Java, e.g., Serializable, Clonnable and Remote, are used to indicate something special to compiler or JVM.

So if JVM finds a class implements Serializable interface, it does some special operation on it.

Similarly, if JVM finds a class is implementing Clonnable it performs some operation to support cloning.

Marker interface indicates a signal or a command to Compiler or JVM.

Marker Interface – Classifying Code

Marker interface is a good way to classify code. You can create marker interface to logically divide your code and if you have your own tool, then you can perform some pre-processing operation on those classes.

Marker Interface – Usage

Some of the most use cases of marker interfaces are given below:

  • Marker interfaces are particularly useful for developing API and framework like Spring or Struts.
  • After introduction of annotation in Java 5, annotation is again a better choice than marker interface.
  • JUnit is a perfect example of using annotation e.g. @Test for specifying a test class. Same can also be achieved by using Test marker interface.
  • One more use of marker interface in Java can be commenting. So you can use marker interface for commenting on your source file.
  • A marker interface gives guarantee to thread-safety and any modification should not violate that.
  • Marker interface can also help code coverage or code review tool to find bugs based on a specified behavior of marker interface.
  • Again annotations are better choice and @ThreadSafe looks lot better than implementing ThraedSafe marker interface.

Implementing Marker Interface

Let’s take an example, a payment can be made for a transaction using cheque, bank draft, debit card, credit card, net banking, etc.

Therefore, during runtime it is decided which method is used to make the actual payment, i.e., using cheque or draft or card or net banking.

Create a BankDraft interface to indicate that payment will be made via bank draft.

public interface BankDraft {
}

Create a Cheque interface to indicate that payment will be made via cheque.

public interface Cheque {
}

Let’s say payment is made via cheque. So the below Payment class implements Cheque interface.

Notice we have put both ways to handle payment methods but will be decided during runtime.

public class PaymentC implements Cheque {

	public void paymentByCheque() {
		System.out.println("Payment by Cheque.");
	}

	public void paymentByBankDraft() {
		System.out.println("Payment by BankDraft.");
	}

}

Create a main class to test the scenario. We check the Payment class of which interface type and the appropriate output will be displayed.

public class MarkerInterfaceCApp {

	public static void main(String[] args) {
		PaymentC payment = new PaymentC();
		
		if (payment instanceof Cheque) {
			System.out.println("payment is instance of Cheque.");
		}
		
		if (payment instanceof BankDraft) {
			System.out.println("payment is instance of BankDraft.");
		}
	}

}

By executing the above main class, you will get below output:

payment is instance of Cheque.

Now Payment implements BankDraft interface.

public class PaymentB implements BankDraft {

	public void paymentByCheque() {
		System.out.println("Payment by Cheque.");
	}

	public void paymentByBankDraft() {
		System.out.println("Payment by BankDraft.");
	}

}

Next create a main class to test the scenario.

public class MarkerInterfaceBApp {

	public static void main(String[] args) {
		PaymentB payment = new PaymentB();

		if (payment instanceof Cheque) {
			System.out.println("payment is instance of Cheque.");
		}

		if (payment instanceof BankDraft) {
			System.out.println("payment is instance of BankDraft.");
		}
	}

}

By executing the above main class, you will see below output.

payment is instance of BankDraft.

Therefore from the above output we saw that the output is displayed based on the type of interface.

Hope you got an idea on marker interface example in Java.

Source Code

Download

Leave a Reply

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