• Post Reply Bookmark Topic Watch Topic
  • New Topic

Null Interface  RSS feed

 
Prasath Thirumoorthy
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,
In Java there are some Interfaces which has only the semantics without states & operations ie without data members & member funtions.It contains only the name of the interface for eg like Serializable, Cloneable

***************************************************************
public interface Serializable extends java.io.Serializable {
}
***************************************************************
public interface Cloneable {
}
***************************************************************
Is there any specific names for this kind of interfaces and when use this kind of interfaces v r not implementing anything Just v put in the class as implements Serializable so how this Serialization is done.This is done by Compiler is it.
Plz Clear my doubt !!!

Thanks & Regards
Prasath
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These are sometimes called marker interfaces. They provide some binary information to anyone who cares to ask.



Implementing Serializable just tells the serializer (I just made up that name) that the object would like to be serialized.

Most interfaces have method declarations and implementing classes promise to implement those methods. Markers just declare some intention like "Yes, I'd like to be serialized!".
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Declaring that a class implements an empty interface like Serializable or Cloneable is just a tagging mechanism to indicate that the class also defines things that do what a Serializable or Cloneable thing should do.

The compiler doesn't implement anything. It's up to the programmer to correctly implement the behavior that implementing the particular interface is supposed to mean.

In other words, if my class is declared to implement Cloneable, then I'd better make sure that it actually does - that the clone() method is properly implemented and doesn't just throw a CloneNotSupportedException. Throwing such an exception from the clone() method, while claiming to implement Cloneable, wouldn't be a very friendly thing to do.

Similarly, if my class implements Serializable, then I'd better make sure the implementation actually does so correctly.

Does that help?
 
Prasath Thirumoorthy
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mr Stan James & Mr Dirk Schreckmann
Thanks a lot !!!
so if v declared a class which implements these kind of Interfaces for eg consider this

public class ImplementsSerializable implements {
public ImplementsSerializable (){
//Creates instance..
}
//data members

//member funtions
}
this is enough to do
when objects of this class are invoked It get serialized automatically is it !!
Clear Me !!! Plz
don t mistake it
If this is a stupid Question
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dirk caught me being a bit too short - sometimes when you implement a marker or tag interface you have to do more work to make sure it will really work. I haven't made anything serializable since the big Y2K scare, so I won't pretend to know the rules for implementing that. Look for a Sun tutorial or javadoc on serializing or any other marker interface you use.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops, forgot to say: Don't worry about stupid questions! There's no penalty for being the most recent person to wonder about something. I've been coding for 26 years and nearly every day I'm the most recent person to realize I don't know how to do something.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!