Solved

marker interfaces

Posted on 2011-09-06
6
416 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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:

762 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

20 Experts available now in Live!

Get 1:1 Help Now