Every Marker interface is an empty interface. Is vice-versa also correct?
-
4Possible duplicate of [Marker Interfaces in Java?](http://stackoverflow.com/questions/25850328/marker-interfaces-in-java) – PM 77-1 Jan 25 '16 at 05:42
-
**Not vice-versa** and see to get a clear concept http://stackoverflow.com/questions/25850328/marker-interfaces-in-java – mmuzahid Jan 25 '16 at 11:58
4 Answers
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:
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)

- 99
- 5
-
Your assertion that it is not possible to create a user-defined marker interface is based on a narrow definition of "marker interface" that is not the commonly accepted definition. – Stephen C Jan 25 '16 at 11:50
-
@StephenC My answer is oriented to what a marker interface is and not to what the marker interface pattern is.I think it is just like the difference between iterator in java and iterator design pattern.I can say that it is not possible to create user defined marker interface in java as ,if you do so you should be able to instruct the jvm or you should be able to re write java api's. – Sudheep Vallipoyil Jan 26 '16 at 19:56
-
Why don't you just try it. Try and compile this: "public interface MyMarker {}". The fact that you write / compile / use your own marker interfaces is not a problem for the JVM. Obviously, the JVM doesn't understand what `MyMarker` *means* as a marker, but it doesn't >>need<< to. It is entirely up to me to implement that; e.g. by using `instanceof MyMarker` and doing something. – Stephen C Jan 26 '16 at 22:28
-
@StephenC I don't think if you are creating public interface MyMarker ,it is not a marker interface , it is an empty interface and also the question was regarding the difference between 2.You can then ask why we need to create an empty interface .It can be a design decision like just keeping some type for later modifications or an intermediate interface which extends another one.Or else we can write one as no one will complain by doing so. – Sudheep Vallipoyil Jan 27 '16 at 05:55
-
By the >>normal<< definition, yes `MyMarker` >>is<< a marker interface. Please read the Wikipedia article for the >>normal<< definition. (I have quoted it in my question, and **BOLDED** the relevant part.) Normally, you would write some other code that understands what it means, but just specify what the marker interface means is sufficient. Also, see my answer for the (real!) difference between a marker interface and an empty interface. It is subtle. – Stephen C Jan 27 '16 at 06:34
-
I read that wiki already.in that i never found anything saying If an interface is empty it is a marker interface!. It just tells that the marker interface need not to contain a method as a typical(not mentioning as all other) interface will. Also with the introduction of default methods even it is possible for the java team can provide a marker interface which can contain a default method also. – Sudheep Vallipoyil Jan 27 '16 at 08:27
-
That is correct. It doesn't says that. Indeed, it says a *hybrid* marker interface combines "marker-ness" and supplies methods. But that's not my point. The point I am trying to make is that it DOES NOT say that only the JVM / Java standard library can define marker interfaces. Your assertion that only the Java team can define marker interfaces has no basis. Or at least, none that you have shown us. – Stephen C Jan 27 '16 at 11:22
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 !)

- 99
- 5
-
I have no argument with this definition. What I have an argument with is this statement in your other Answer. *"It is not possible to create a user-defined marker interface."*. So far, you have provided no source to back this up. Certainly, the definition you have quoted above from Joshua Bloch does not support it. – Stephen C Jan 28 '16 at 14:25
-
*"i do't think the answer should be something like copying some definitions from wiki"* - There was a purpose to quoting the definition that you either don't understand or won't acknowledge. That purpose was to refute an (IMO) incorrect statement in your Answer. – Stephen C Jan 28 '16 at 14:28
-
Or to put it another way, please explain / justify based on some accepted definition why `public interface MyMarker {}` is NOT a valid (user-defined) marker interface. Explain / justify why a marker interface >>has to<< give an indication >>to the JVM<< of anything ... let alone behavior. – Stephen C Jan 28 '16 at 14:37
-
And finally, the original question is all about definitions; i.e. what the term "marker interface" means. So an answer that doesn't bother to cite definitions is ... well ... a statement of the Answer's author's opinion. Unfortunately, in the IT world, most definitions are (or start out) as someone's opinion. The difference is that widely published definitions / opinions on the meaning of terms >>tend<< to be accepted by others, especially when the author or the source is well-known. – Stephen C Jan 28 '16 at 14:49
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!!