Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

marker interfaces

Posted on 2011-09-06
6
Medium Priority
?
429 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Implementing Azure Infrastructure Exam 70-533

This course is designed to familiarize and instruct students in the content that is covered by Microsoft Exam 70-533, Implementing Microsoft Azure Solutions. It focuses on all the November 2016 objective domain topics.

 
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

How To Install Bash on Windows 10

Windows’ budding partnership with Canonical has certainly led to some great improvements. One of them being the ability to use Bash on your Windows machine without third party applications! This might be one of the greatest things a cloud engineer in a Windows environment can do!

Question has a verified solution.

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

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

670 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