Gr8Warrior
Gr8Warrior

Reputation: 719

Difference between Marker interface and empty interface?

Every Marker interface is an empty interface. Is vice-versa also correct?

Upvotes: 0

Views: 2624

Answers (4)

user25546
user25546

Reputation: 1

Your wrong!!! I do not know where you are getting this information at? From java SE 7 Programmers Study Guide mentions that “interface is a public set of methods that must be implemented by the class that uses the interface.” An interface is an interface, if it has changed I should read this in this type of book mention above. If an interface like Cloneable has no public methods, means it does not have public methods, but any class that implements it must implement the Object.clone() as public. This is what it means!!! Please, don’t make something so simple into a complex discussion, especially at an interview!!

Upvotes: -2

Stephen C
Stephen C

Reputation: 718836

Every Marker interface is an empty interface. Is vice-versa also correct?

Nope. An empty interface that has no meaning is not a marker interface ... in any useful sense. A marker interface has to denote something meaningful about the class that implements it.

And, in fact, according to some definitions a (hybrid) marker interface can contain methods (see the Wikipedia definition below). So a marker interface isn't necessarily an empty interface.


@Sudheep Vallipoyil says in his answer.

1) It is not possible to create a user-defined marker interface.

I disagree. That is based on a narrow definition of "marker interface" that is not the commonly accepted definition.

By contrast, here's how Wikipedia defines "marker interface" and the corresponding design pattern.

"The marker interface pattern is a design pattern in computer science, used with languages that provide run-time type information about objects. It provides a means to associate metadata with a class where the language does not have explicit support for such metadata."

"To use this pattern, a class implements a marker interface (also called tagging interface), and methods that interact with instances of that class test for the existence of the interface. Whereas a typical interface specifies functionality (in the form of method declarations) that an implementing class must support, a marker interface need not do so. The mere presence of such an interface indicates specific behavior on the part of the implementing class. Hybrid interfaces, which both act as markers and specify required methods, are possible but may prove confusing if improperly used."

A second definition from Joshua Bloch says:

"A marker interface is an interface that contains no method declaration, but merely designates (or "marks") a class that implements the interface as having some property."

As you can see, in normal accepted usage, the term "marker interface" is not reserved for interfaces defined by the JVM. Anyone can define one.

See also:

Upvotes: 1

Sudheep Vallipoyil
Sudheep Vallipoyil

Reputation: 99

This answer is to comment on @Stephen C on his answer

1.The question is not what is Marker interface pattern.(It is like asking what is iterator and what is iterator pattern)

2.The wiki definition is for the 'marker interface design pattern' not for marker interface concept in java

3.from effective java by Joshua you can find the following definition for marker interface

"A marker interface is an interface that contains no method declaration,but merely designates(or "marks") a class that implements the interface as having some property."

He also explained why Set(collection frame work) which intact an empty interface is not a Marker interface.

i do't think the answer should be something like copying some definitions from wiki and explain the same(that also the unrelated one !)

Upvotes: 0

Sudheep Vallipoyil
Sudheep Vallipoyil

Reputation: 99

At first shot we can call an interface as marker interface if it does not contain any members and it is used to give an indication to JVM about the run time behavior of the implementing class. Every empty interface is not a a marker interface as

1)It is not possible to create a user-defined marker interface

2)The empty interface you are writing still can extend any other interfaces which are not empty (then even the empty interface has some inherited members).

It is better to understand like the need to use a marker interface is replaced with the use of annotations from java 5.(by this you can understand what an marker interface meant for)

Upvotes: 0

Related Questions