Solved

marker interfaces

Posted on 2011-09-06
6
418 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:
gurvinder372 earned 250 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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 250 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
windows explorer path to command prompt 5 43
HashTable highest marks enumeration alternative 9 37
expectj telnet failing 5 35
by zero exception 10 39
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

813 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now