C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » Java programming language

Marker Interface in Java

Learn: What is marker interface in java? Why it is used? Define and Elaborate marker interface in java?
Submitted by Preeti Jain, on February 02, 2018

Java - Marker Interface

An interface with empty implementation (i.e It contains nothing like methods, variables etc.) is known as Marker Interface.

Example:

interface Marker{
}

Marker interface is also known as tag interface.

Whenever user defined or language defines class implements such types of interfaces then our objects will get special functionalities or extra abilities.

Marker interface won't contain any method then how the objects will get that special ability. This thing should be come in mind. (i.e. JVM is only responsible to provide required ability in marker interfaces).

We can create our own marker interface but customization (changes) of JVM is required.

Java provides several marker interfaces. Few of them we are going to discuss. It is a part of JDK API and come from the different package.

  1. Cloneable
  2. RandomAcess
  3. Remote
  4. Serializable etc

1) Cloneable Interface

When we implements Cloneable interface our object will be in a position to provide exactly same duplicate objects.

Example: How to copy object with the help of clone() method?

class CloneableClass implements Cloneable {
	int i ,j;
	public static void main(String[] args) throws CloneNotSupportedException{
		CloneableClass cc = new CloneableClass();
		CloneableClass co = (CloneableClass)cc.clone();

		System.out.println("value of i " +(cc.i) + " and value of j is " + (cc.j));
		System.out.println("value of i " +(co.i) + " and value of j is " + (co.j));
	}
}

Output

D:\Java Articles>javac CloneableClass.java

D:\Java Articles>java CloneableClass
value of i 0 and value of j is 0
value of i 0 and value of j is 0

2) RandomAccess Interface

When we implements RandomAccess interface then our object will be in a position to access a random element from array.

3) Remote Interface

If i create an object and places that object on one machine and try to access that object from another machine then we will have to move towards Remote interface. To make remote interface we will have to implements Remote interface.

4) Serializable Interface

When we implements Serializable interface we can send objects across the network.



Liked this article? Do share with your friends :)


COMMENTS