aspose file tools*
The moose likes Beginning Java and the fly likes Marker Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Marker Interface" Watch "Marker Interface" New topic
Author

Marker Interface

Mayur Malhotra
Greenhorn

Joined: Mar 18, 2008
Posts: 17
How can we create a user defined marker interface?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Now Annotations exist I'm not sure we would. But:



or is you question how can we use a custom marker interface?
[ March 18, 2008: Message edited by: Paul Sturrock ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Neha Agarwal
Greenhorn

Joined: Mar 12, 2008
Posts: 16
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 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.

To implement the same we have to write the same code as for simple interface as
public class MyClass implements Cloneable {
// ...
public Object clone() { // clone this object
// get a clone of this:
MyClass that= (MyClass)super.clone();
// copy deep clones of reference members from this to that
return that;
}
}

I hope this answers your question
Regards
Neha
Mayur Malhotra
Greenhorn

Joined: Mar 18, 2008
Posts: 17
Hi,

I am aware of what a marker interface is and what it is used for but my question is :Can we Create our own marker interface(user defined marker interface) ,apart from the standard ones which are provided by java.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Of course you can, here is one :


[My Blog]
All roads lead to JavaRanch
Mayur Malhotra
Greenhorn

Joined: Mar 18, 2008
Posts: 17
But how will the java or jvm knows that this interface

public interface Mayur {
}

is a marker interface.I mean how jvm will identify that any class which implements this interface must have some special capabilities like Serializabl e or Cloneable has.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42600
    
  65
The JVM won't know anything about any newly declared marker interfaces. But any code you write can make use of it by checking "if (myObject instanceof Mayur)".


Ping & DNS - my free Android networking tools app
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

I mean how jvm will identify that any class which implements this interface must have some special capabilities like Serializabl e or Cloneable has

I don't think this is possible. You will have to do it yourself, using reflection. But would you do such a thing anyway ?
Mayur Malhotra
Greenhorn

Joined: Mar 18, 2008
Posts: 17
Ok I can check that :myObject instanceof Mayur to confirm whether my class implements my marker interface but what about if a class implements Serializable or Cloneable its not checking into it whether the class implements Serializable or not in fact it just makes Object of that class(which implements serializable) available for serailization .can I have such a mechanism through my own marker interface if so how?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42600
    
  65
what about if a class implements Serializable or Cloneable its not checking into it whether the class implements Serializable or not in fact it just makes Object of that class(which implements serializable) available for serailization.

I may be misunderstanding what you're saying, but if a class doesn't implement Serializable (Cloneable), then it won't get serialized (cloned). So yes, the JVM (or code in the class libraries) does indeed check whether these interfaces are implemented. See this thread for a discussion of how Cloneable works.
Mayur Malhotra
Greenhorn

Joined: Mar 18, 2008
Posts: 17
I think now you are clear with my question i.e.

I mean to say If a create an empty interface then it wont serve purpose of marker interface so in order to make it a marker interface what I have to do to give an indication to jvm that this interface is not a normal empty interface its a marker interface?

Or Consider if i need to create a Serializable1 interface of my own having the same functionality what actual Serializable has then what all steps do i have to follow?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42600
    
  65
if i need to create a Serializable1 interface of my own having the same functionality what actual Serializable has then what all steps do i have to follow?

You can't. Serialization is done by the JVM, and unless you are prepared to change the JVM (which would make it incompatible with other JVMs) there's nothing you do about that.

Your custom marker interface will only be recognized by code that tests for it using the instanceof operator.

And, of course, these days you shouldn't create new marker interfaces at all. That's what annotations are for.

(If you're actually serious about changing the way serialization works, then your classes can implement the Externalizable interface, which let's you control the details of serialization in a very fine-grained way.)
Mayur Malhotra
Greenhorn

Joined: Mar 18, 2008
Posts: 17
Or Consider if i need to create a marker interface of my own then what all steps do i have to follow in order to inform jvm that I ahve Created a marker interface and not a simple blank interace?Or else I cannot create such a marker interface of my own???
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42600
    
  65
There is no difference between an empty interface and a marker interface - they're the same thing. You don't need to tell the JVM about it - your code can just start using it. The JVM will never know about it, though, and won't ever be able to do anything special about classes implementing it. That's all happening in your code, or in other people's code that uses the interface.
Mayur Malhotra
Greenhorn

Joined: Mar 18, 2008
Posts: 17
So in the same case where i will provide special functionality what my interface has to serve.Say i want to create a marker interface
Which does connection pooling?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42600
    
  65
I think you are confused over what marker interfaces can and can not do. How would an interface that contains no methods implement connection pooling?

You can create an interface that has methods for everything a connection pool needs to do (although I don't see the benefit of doing that).
Mayur Malhotra
Greenhorn

Joined: Mar 18, 2008
Posts: 17
Can you tell me what I can/cannot do with marker interfaces?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42600
    
  65
You can do whatever you can think of, as long as it's sufficient to check whether an object is an instance of your interface. But the object is passive - it can't do anything itself, since it has no methods that implement functionality (at least none that are part of the marker interface; it will most likely have other methods).

What prompted your interest in marker interfaces? If you tell us what you're trying to do, we can probably suggest a better solution.
Mahesh Bamane
Ranch Hand

Joined: Mar 12, 2008
Posts: 68
See Mayur basically we use marker interface to make special type of object.
If we take an example of a book shop where we find different type of books, in that suppose you want to display all the books which are related to Java then you can use marker interface there.
You can create a Interface say JavaBooks which wont be having any methods(A blank interface), then all the classes(say for example I say MyJavaBook) implements this blank Interface.
And then while displaying all the books related to Java you can chk using instancOf whether the book is of type JavaBooks(Marker Interface which we created previously).


There is nothing like trying, either you do it or you don't.
SCJP 1.5
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42600
    
  65
If we take an example of a book shop where we find different type of books, in that suppose you want to display all the books which are related to Java then you can use marker interface there.


That would be an example of when not to use a marker interface. (As mentioned before, no new marker interfaces should be defined to begin with -use annotations instead- but this is a different issue.)

Better ways of distinguishing between Java books and regular books would be to either extend the Book class (like "class JavaBook extends Book"), or to keep a discriminating field in the Book class (like "if (myBook.getType() == Book.JAVA)").
Mahesh Bamane
Ranch Hand

Joined: Mar 12, 2008
Posts: 68
Thanks a lot Ulf,
It was just a quick thought.
Could you please give us a proper example of Marker interface?
Wentao Liang
Greenhorn

Joined: Oct 05, 2007
Posts: 13
I am amazing....
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42600
    
  65
Originally posted by Mayur Malhotra:
Hi, can you give me a practical scenario where we have a need to create our own marker interface.


Mayur, please UseTheForumNotEmail.

There is never a need to use marker interfaces; other ways of accomplishing the same are always possible. If you study how Java annotations are used (which are the way of handling class metadata starting with Java 5) you'll see functionalities where they might be considered useful.

Read this article for the perspective of someone who worked on the Java class libraries (bottom line: they should rarely be used).
Mayur Malhotra
Greenhorn

Joined: Mar 18, 2008
Posts: 17
But Can we Create our own Marker interface ,I mean does jvm allow us to do that or not?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42600
    
  65
I'm not sure I understand what you're asking. Both Paul and Christophe gave examples of how to create one, so obviously the JVM allows you to do this.
Mayur Malhotra
Greenhorn

Joined: Mar 18, 2008
Posts: 17
So i say i 've created a marker interface i will create an empty interface and to check for what kind of functionality it provides we need to check by refletion in its implementation class?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42600
    
  65
Haven't we covered all this before? You can use reflection, but it's easier to use the instanceof operator.
Mayur Malhotra
Greenhorn

Joined: Mar 18, 2008
Posts: 17
In fact I just want you ask you whether my assumption or picture about marker interface is clear or not.But still i have a small doubt "Can we say the same idea of providing logic(in implementation class) is present for Standard Marker interface like Serializable and Cloneable or they also have something more added to them"?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42600
    
  65
I'm not sure what you mean by "something more". If you want a marker interface -no matter who defined it, or where it got defined- to be good for something, you'll need to provide the code to do that somewhere.
Mayur Malhotra
Greenhorn

Joined: Mar 18, 2008
Posts: 17
By something more i mean to say Like a serilizable interface makes an object capable of serialization how it is done internally by jvm as if remember clearly I have not written any implementation class of serializable to check for instanceOf operator?So how jvm knows That serializable means something different from normal interface?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42600
    
  65
See the link I posted above for a more in-depth discussion of how serialization works. The code that implements it is part of the Java class libraries.
Mayur Malhotra
Greenhorn

Joined: Mar 18, 2008
Posts: 17
Hi can you please send the link again as I need to cross-verify which link you asked me to refer
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42600
    
  65
I have only posted a single link in this entire discussion, and it's still there ...
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by Mayur Malhotra:
By something more i mean to say Like a serilizable interface makes an object capable of serialization how it is done internally by jvm as if remember clearly I have not written any implementation class of serializable to check for instanceOf operator?So how jvm knows That serializable means something different from normal interface?


This question has been asked many times on this thread, and the answer has been given each time. I think you're having a hard time understanding because the answer is so simple!

A "marker interface" isn't special in any way; it's just a convention used a few times in the Java APIs. Take "Cloneable" as an example. To clone an object, you call the clone() method inherited from Object, right? The clone() method effectively looks like this:



That's all. There's no magic whatsoever about the "Cloneable" interface; it's just a design pattern used by this one single method.

I said "effectively" because if you go look at the JDK source, you may indeed find that all of clone() is implemented as native code. But this is still the way it works; it doesn't make Cloneable any more magical.

Similarly, ObjectOutputStream.writeObject() would do something like



Again, Serializable isn't magic at all -- it's just something this one method checks for.

So to make your own marker interface, all you need to do is to write a method, somewhere, that treats objects differently depending on whether or not they implement this interface. That's all!


[Jess in Action][AskingGoodQuestions]
Mayur Malhotra
Greenhorn

Joined: Mar 18, 2008
Posts: 17
Hi thanks for the explanation but still I have small doubt in the code to implement Cloneable why we check for "this implements Cloneable" since its an empty interface so what difference it makes if we check or not check that whether the Object to be cloned implements Cloneable or not?Can you please clear my doubt?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42600
    
  65
You're still completely missing the point of marker interfaces. You can think of them as a boolean attribute about objects of a class:

Serializable: May objects of this class be serialized or not?

Cloneable: May objects of this class be cloned or not?

The instanceof operator effectively checks the value of this boolean attribute. As was said repeatedly before, the functionality is not in the interface, it's in the code that performs the instanceof check.

Your confusion about this highlights the fact why marker interfaces were a bad idea - they're abusing a language feature.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

What I can think of is a top class, with many subclasses. You want to give a functionality common to many subclasses, but not all. So you put this functionality in the top class, and check that the subclass calling it has the right to do so, using a marker interface.



Here, you can see that the RanchTribe cannot revolt. I guess that the reasonning behing Cloneable is the same. Giving all Java objects a chance to clone, but restricting it to Cloneable object only.
Mayur Malhotra
Greenhorn

Joined: Mar 18, 2008
Posts: 17
I thanks for detailed explanation about marker interface Your reply was excellent.
From your I think you want to make the point is that marker Interface has nothing special in it ,infact its just a tag or (we can say a flag or bechmark) to check before performing Operations like Serilizing or Cloning an object.Kindly let me know whether my view about marker interface is correct or not also do add any further explanation in case i missed something.
antony suresh
Greenhorn

Joined: May 18, 2007
Posts: 1
Marker interface makes the user to make sure that the declared method(writeObject or clone()) with that kind of parameter is purposely or not. If you like to clone or serialiaze a object, you must use that method by implementing that cloneable or serializable interface.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
Welcome to the Ranch, Antony Suresh.

You may have opened a can of worms by resurrecting this old thread; look at what preceded your post!

Once a thread has been inactive for more than a week, it's probably kindest to let it sleep for ever.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Marker Interface