Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Marker Interface

 
veeramani velayutham
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
marker interface does not contain method and variable. then why are we using marker interface.
 
sanjeevmehra mehra
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marker interface does not have any member but there is always reason to use it.
it's tag or green signal to allow to perform the task for which we inherit class/interface from it.
 
veeramani velayutham
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can u plz. explain in detail.
 
Edward Kenworthy
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<p>Personally I think it's a really poor idea: very bad OO design as it splits responsibility.

<p>Probably the most obvious example is with java.lang.Object and java.lang.Clonable: Object has a clone method but it wont work unless the class also implements Clonable.

<p>The way it should have done was to place the clone method on the Clonable interface and have a protected clone helper method on Object. One of several poor OO design decisions in Java I'm afraid (my other big bug bears if you're interested are: deprecated methods are never deleted and pretty much the whole of Java 1.5

Edward
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
clone() is a bad design, but I wouldn't say that marker interfaces in general are always a bad thing.

When a class implements a marker interface X, it's just a way of saying "I support some feature X" which isn't expressed as a set of methods. For example, implementing the java.io.Serializable marker interface is a signal to the JVM that a class will allow itself to be serialized.

Whenever there's a marker interface X, there's code someplace which says

 
Edward Kenworthy
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to disagree: serializable should implement an interface with a "serialize" method. A default can be provide by Object.
 
Pratik Thakare
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a beginner i might be wrong but according to me

Marker interface is also called as empty interface. it does not consist of any method. example Serializable and clonable are two marker interface.

if you are using Serializable or clonable interface it means (it indicates) that your class object are is serializable or clonable.

It can be used as an indicator.

IF i am wrong please reply back
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
veeramani velayutham wrote:marker interface does not contain method

actually it has...
JLS =>

If an interface has no direct superinterfaces, then the interface implicitly declares a public abstract member method m with signature ss, return type rr, and throws clause t corresponding to each public instance method mm with signature ss, return type rr, and throws clause tt declared in Object, unless a method with the same signature, same return type, and a compatible throws clause is explicitly declared by the interface. It is a compile-time error if the interface explicitly declares such a method m in the case where m is declared to be final in Object.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pratik Thakare wrote:
if you are using Serializable or clonable interface it means (it indicates) that your class object are is serializable or clonable.

If a class implements Serializable and it dont contain any other object which is not serializable, then yes, its object can be serializable
Serialization is a deep copy process. so all object references from the object also should be Serializable.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic