Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

marker interfaces

Posted on 2011-09-06
6
Medium Priority
?
431 Views
Last Modified: 2012-05-12
I would like to now more about marker interfaces. why, how, when we use them, advantages, disadvantages of using them. Thanks in advance
0
Comment
Question by:gudii9
  • 4
6 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36493429
tread here:
http://www.roseindia.net/java/master-java/interface.shtml
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.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36493438
this is about srializable interfcae as an example of marker interfaces
http://javarevisited.blogspot.com/2011/04/top-10-java-serialization-interview.html

This explains why Serializable marker interface is useful:
2) How many methods Serializable has? If no method then what is the purpose of Serializable interface?
Serializable interface exists in java.io  package and forms core of java serialization mechanism. It doesn't have any method and also called Marker Interface. When your class implements Serializable interface it becomes Serializable in Java and gives compiler an indication that use Java Serialization mechanism to serialize this object.

5) What will happen if one of the members in the class doesn't implement Serializable interface?
If you try to serialize an object of a class which implements Serializable, but the object includes a reference to an non- Serializable class then a ‘NotSerializableException’ will be thrown at runtime and this is why I always put a SerializableAlert (comment section in my code) to instruct developer to remember this fact while adding a new field in a Serializable class.
0
 
LVL 40

Accepted Solution

by:
Gurvinder Pal Singh earned 1000 total points
ID: 36493519
In simple words,
Marker Interface is just for marking a class.
public class MarkableClass implements Marker

You can always check later that whether an instance of 'MarkableClass' class was of type 'Marker' or not by
MarkerClass mc = new MarkerClass();

if ( mc instanceOf Marker)
{
   // should be true
}

This is usually done to make server or framework aware of special classes or class with specific purpose.
For example, if I Mark a class with 'Serializable' Interface, then it tells the server that, this class instance is supposed to be serialized before using in network communications.
Also, if i Mark a class as 'MouseListener', then it tells my event management framework that every mouse event is supposed to be handled by this class
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Author Comment

by:gudii9
ID: 36589749
>>>>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.




how empty interface provide functionality. It does not make sense to me still
>>>If you try to serialize an object of a class which implements Serializable, but the object includes a reference to an non- Serializable class

So does all the referenced  object references needs be serialized within a serialized class


>>>>if i Mark a class as 'MouseListener', then it tells my event management framework that every mouse event is supposed to be handled by this class

>>>every mouse event is supposed to be handled by this class

do you mean
every mouse event is supposed to be handled for this class by the framework or server?


0
 
LVL 47

Expert Comment

by:for_yan
ID: 36589784
No, if i Mark a class as 'MouseListener' ...

This is absolutely simple:

If I write at the tlop that this class implemnets MouseListener
then this class needs to have implemnentations of all 5 methods which are required by this interface.
Thes implementstion can be just empty - nothing bettween the braces - but they should be. Period.
Otherwise you'll get compiler error.
No magic about it.



0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 1000 total points
ID: 36589840
The designers of Java considered that there are some cases when certain java classes
would make no sense to be srialized/derialize and in some cases
these seialization./derilaization may cause issues with what they call security - which means that field
which are designated private after some transfrmations will become accessible by those who normally would not access them.

what are alll those considerations and why they thought it would be a dnager yopu can read in the lengthy reply here:
http://java.sun.com/javase/technologies/core/basic/serializationFAQ.jsp#whyserial

As a result they canm e to the conclusion - let the author of  a class thuink twicw before he allows his calss
to be serialized/derialized
And only if he/she decides it is possible - and marks this fact by adding serializable then
instances of thsi class can be serialized - this check that the author of the class explicitly states that this class
can be srialized - that what caused this requirement


 
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question