• Post Reply Bookmark Topic Watch Topic
  • New Topic

What for marker or tag interface is used?  RSS feed

 
Gangadhar Reddy Gangireddy
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
We all know that, marker or tag interface such as "Serializable" does not have either data members or member functions. Then what is the use of implementing such interfaces, when we dont have any member functions to change the behaviour of marker interface. I would be thankful to you, if u could give me proper reason for this. Because I have been looking for this answer for more then 6 months.

Waiting for reply
Gangi
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason you have been looking for so long is because there is no answer - at least, not a legitimate one. There is some fancy reiteration of what appears in literature, but as you may be suspecting, the reasoning and/or explanation is incredibly flawed. If you are prepared to conjecture that there is no answer (as I am proposing), it might help then to pose the question to yourself, "Why shouldn't a marker interface ever be used?".
 
Louise J Elliott
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A marker interface is used more for information than anything else. By saying that your class implements serializable you are telling other code that it contains only member variables which can be serialized (or are transient). That allows your other code to ensure that it only accepts serializable instances.

For example, if you are sending an object as a JMS message inside ObjectMessage then it must be serializable so that it can be sent successfully. This can be checked at compile time.

Having said that there are some issues - you can declare a class to be Serializable when, in fact, it isn't. This problem would not be found until run time. Also, if you declare a class to be serializable and then you extend that class then your child class also implements Serializable and you can't prevent that.

Marker interfaces are useful when you want to give compile time information about the type of class this is when no methods are required. So, you might have a Shape marker interface which is implemented by Square and Circle but not actually share any methods.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello GangiAtJavaRanch -

Welcome to JavaRanch.

On your way in you may have missed that JavaRanch has a policy on display names, and yours does not comply with it; specifically, a first name and a last name are required. Please adjust it accordingly, which you can do right here. Thanks for your prompt attention to this matter.-

As to your question, it comes up frequently. A search through the old posts will bring up many discussions, and a search of the FAQ will find this page.
[ June 20, 2006: Message edited by: Ulf Dittmer ]
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16047
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marker interfaces are one of the old ad-hoc ways to specify metadata in Java. If you're thinking about making your own marker interfaces, don't - write an annotation instead.

See this recent thread about the same subject.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Serializable is a special way of invoking special behavior from the Java environment. Same with Cloneable.

These are more 'markers' than marker interfaces. Just like SerialVersionUID is not your standard variable.
 
Campbell Ritchie
Marshal
Posts: 56221
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper Young has quoted a recent thread of relevance.
Here is another from a week ago.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!