Solved

marker interfaces

Posted on 2011-09-06
6
419 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 214
How to increase Spring boot/Tomcat max file upload size 2 157
hibernate insert example 13 34
configure dependency in POM for new database 3 34
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

828 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