• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Marker Interface

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can we create a user defined marker interface?
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course you can, here is one :
 
Mayur Malhotra
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)".
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you tell me what I can/cannot do with marker interfaces?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Ulf,
It was just a quick thought.
Could you please give us a proper example of Marker interface?
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am amazing....
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But Can we Create our own Marker interface ,I mean does jvm allow us to do that or not?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Haven't we covered all this before? You can use reflection, but it's easier to use the instanceof operator.
 
Mayur Malhotra
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi can you please send the link again as I need to cross-verify which link you asked me to refer
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have only posted a single link in this entire discussion, and it's still there ...
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
Mayur Malhotra
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 80133
418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 wasn't selected to go to mars. This tiny ad got in ahead of me:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic