Tuesday, March 10, 2009

The Purpose of the Marker Interface

One of the "clean" features of the Java programming language is that it mandates a separation

between interfaces (pure behavior) and classes (state and behavior). Interfaces are used in Java to

specify the behavior of derived classes.

Often you will come across interfaces in Java that have no behavior. In other words, they are just

empty interface definitions. These are known as marker interfaces. Some examples of marker

interfaces in the Java API include:


- java,lang.Cloneable
- java,io.Serializable
- SingleThreadMode
- java.util.EventListener

Marker interfaces are also called "tag" interfaces since they tag all the derived classes into a category

based on their purpose. For example, all classes that implement the Cloneable interface can be cloned

(i.e., the clone() method can be called on them). The Java compiler checks to make sure that if the

clone() method is called on a class and the class implements the Cloneable interface. For example,

consider the following call to the clone() method on an object o:


SomeObject o = new SomeObject();
SomeObject ref = (SomeObject)(o.clone());

If the class SomeObject does not implement the interface Cloneable (and Cloneable is not

implemented by any of the superclasses that SomeObject inherits from), the compiler will mark this

line as an error. This is because the clone() method may only be called by objects of type "Cloneable."

Hence, even though Cloneable is an empty interface, it serves an important purpose.
--------------------------------------------------------------------------------------------------------
java programming language
interface is nothing but the collection of methods with empty implementations and constants variables

( variables with static and final declarations ). All the methods in an interface are "public and abstract"

by default. Since interfaces are abstract in nature so they can not be directly instantiated. To define

the methods of an interface the keyword "implements" is used. Interfaces are similar to abstract

classes but the major difference between these two is that interface have all the methods abstract

while in case of abstract classes must have at least one abstract method. Interface combines the two

functionality (template and multiple inheritance) of C++ language into one (in itself).

Interface Definition
visibility mode interface interfaceName{
constant variable declarations
abstract method declarations
}

e.g.

public interface RacingCar{
public void startcar (int Obj);
public void changegear (int Obj);
public void incrrace (int Obj);
public void stopcar (int Obj);
}

Marker Interface

In java language programming, interfaces with no methods are known as marker interfaces. Marker

interfaces are Serializable, Clonable, SingleThreadModel, Event listener. Marker Interfaces are

implemented by the classes or their super classes in order to add some functionality.

e.g. Suppose you want to persist (save) the state of an object then you have to implement the

Serializable interface otherwise the compiler
will throw an error. To make more clearly understand the concept of marker interface you should go

through one more example.

Suppose the interface Clonable is neither implemented by a class named Myclass nor it's any super

class, then a call to the method clone() on Myclass's object will give an error. This means, to add this

functionality one should implement the Clonable interface. While the Clonable is an empty interface but

it provides an important functionality.

Difference between Interfaces and abstract classes ?

1 comment:

Unknown said...

It is really a great work and the way in which u r sharing the knowledge is excellent..Thanks for your informative article.
java training in chennai |
java training institutes in chennai