Newbie - I don't understand interface Serializable

  • Follow


The  interface Serializable does not have any methods or variables.
If I create a subclass of an Object class - is this class
Serializable?
For example, let say I define:

public class A extends Object{
int a;
}

Now make the following definition:

public class A extends Object implements Serializable{
int a;
}

What makes the 2nd definition Serializable?

Thanks,

Zalek
0
Reply ZalekBloom (137) 6/28/2008 4:23:07 PM

zalek <zalekbloom@hotmail.com> writes:

> The  interface Serializable does not have any methods or variables.

Correct. It's a "marker interface" which merely marks the class as being
intended for serialization. The serialization code checks for the marker,
and refuses to serialize something without it - not because it couldn't,
but just because it won't - serializing and derserializing something not
build for it could cause unexpected trouble. 

Today it would probably have been implemented as an inherited
annotation.

> If I create a subclass of an Object class - is this class
> Serializable?

If it implements the Serializable interface.

> For example, let say I define:
>
> public class A extends Object{
> int a;
> }

Yes, that class isn't serializable. No need to write the "extends Object".
The class will automatically extend Object if no other superclass is 
given.

> Now make the following definition:
>
> public class A extends Object implements Serializable{
> int a;
> }
>
> What makes the 2nd definition Serializable?

That it implements the interface! That means that the default
serialization code will accept to serialize and deserialize objects
of that class.

/L
-- 
Lasse Reichstein Nielsen
 DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
  'Faith without judgement merely degrades the spirit divine.'
0
Reply lrn (235) 6/28/2008 4:32:58 PM


On Jun 28, 12:32 pm, Lasse Reichstein Nielsen <l...@hotpop.com> wrote:
> zalek <zalekbl...@hotmail.com> writes:
> > The  interface Serializable does not have any methods or variables.
>
> Correct. It's a "marker interface" which merely marks the class as being
> intended for serialization. The serialization code checks for the marker,
> and refuses to serialize something without it - not because it couldn't,
> but just because it won't - serializing and derserializing something not
> build for it could cause unexpected trouble.
>
> Today it would probably have been implemented as an inherited
> annotation.
>
> > If I create a subclass of an Object class - is this class
> > Serializable?
>
> If it implements the Serializable interface.
>
> > For example, let say I define:
>
> > public class A extends Object{
> > int a;
> > }
>
> Yes, that class isn't serializable. No need to write the "extends Object".
> The class will automatically extend Object if no other superclass is
> given.
>
> > Now make the following definition:
>
> > public class A extends Object implements Serializable{
> > int a;
> > }
>
> > What makes the 2nd definition Serializable?
>
> That it implements the interface! That means that the default
> serialization code will accept to serialize and deserialize objects
> of that class.
>
> /L
> --
> Lasse Reichstein Nielsen
>  DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
>   'Faith without judgement merely degrades the spirit divine.'

Thanks!!!

Zalek
0
Reply ZalekBloom (137) 6/28/2008 4:43:26 PM

Lasse Reichstein Nielsen wrote:
> zalek <zalekbloom@hotmail.com> writes:
> 
>> The  interface Serializable does not have any methods or variables.
> 
> Correct. It's a "marker interface" which merely marks the class as being
> intended for serialization. The serialization code checks for the marker,
> and refuses to serialize something without it - not because it couldn't,
> but just because it won't - serializing and derserializing something not
> build for it could cause unexpected trouble. 
> 
> Today it would probably have been implemented as an inherited
> annotation.
Actually, It has uses as a Type, but unfortunately the original 
*ObjectStream API developers saw fit to accept "Object" instead of 
Serializable.
> 
>> If I create a subclass of an Object class - is this class
>> Serializable?
> 
> If it implements the Serializable interface.
> 
>> For example, let say I define:
>>
>> public class A extends Object{
>> int a;
>> }
> 
> Yes, that class isn't serializable. No need to write the "extends Object".
> The class will automatically extend Object if no other superclass is 
> given.
> 
>> Now make the following definition:
>>
>> public class A extends Object implements Serializable{
>> int a;
>> }
>>
>> What makes the 2nd definition Serializable?
> 
> That it implements the interface! That means that the default
> serialization code will accept to serialize and deserialize objects
> of that class.
> 
> /L


-- 
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
0
Reply newsgroup.spamfilter (920) 6/28/2008 5:20:47 PM

Daniel Pitts <newsgroup.spamfilter@virtualinfinity.net> writes:

[Serializable]
> Actually, It has uses as a Type, but unfortunately the original
> *ObjectStream API developers saw fit to accept "Object" instead of
> Serializable.

True, I have used Serializable for parameters to serialization helper
methods.

I would prefer it as an annotation, but I also want to add annotation
restrictions to parameters, e.g., something like:
  void <T extends MyType & @MySerializable> doSomething(T param)

/L
-- 
Lasse Reichstein Nielsen
 DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
  'Faith without judgement merely degrades the spirit divine.'
0
Reply lrn (235) 6/28/2008 5:52:00 PM

zalek wrote:
> The  interface Serializable does not have any methods or variables.
> If I create a subclass of an Object class - is this class
> Serializable?


Lasse gave you some really good answers to your questions.  Here's a 
decent how-to on serialization from Sun.  It explains further how 
serialization actually works and gives some guidelines how to use it.

<http://java.sun.com/developer/technicalArticles/Programming/serialization/>
0
Reply markspace (954) 6/28/2008 6:01:55 PM

Lasse Reichstein Nielsen wrote:
> Daniel Pitts <newsgroup.spamfilter@virtualinfinity.net> writes:
>> Actually, It has uses as a Type, but unfortunately the original
>> *ObjectStream API developers saw fit to accept "Object" instead of
>> Serializable.
> 
> True, I have used Serializable for parameters to serialization helper
> methods.
> 
> I would prefer it as an annotation, but I also want to add annotation
> restrictions to parameters, e.g., something like:
>   void <T extends MyType & @MySerializable> doSomething(T param)

Annotations did not exist when Serializable was invented, so that
was not an option. And probably never will be an option either due
to compatibility.

..NET has it as an annotation (called attribute in .NET terminology).

BTW, if you want to restrict in template then I really think that
interface would fit best. To me restrictions are "is a".

Arne
0
Reply arne6 (9485) 6/28/2008 7:27:24 PM

On Sat, 28 Jun 2008 09:23:07 -0700 (PDT), zalek
<zalekbloom@hotmail.com> wrote, quoted or indirectly quoted someone
who said :

>The  interface Serializable does not have any methods or variables.
>If I create a subclass of an Object class - is this class
>Serializable?
>For example, let say I define:

it is just marker to indicate you have thought through the class to do
the transients etc. needed to make it serialisable.
-- 

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
0
Reply see_website (4858) 6/29/2008 1:29:12 AM

Mark Space wrote:
> zalek wrote:
>> The  interface Serializable does not have any methods or variables.
>> If I create a subclass of an Object class - is this class
>> Serializable?
>
>
> Lasse gave you some really good answers to your questions.  Here's a
> decent how-to on serialization from Sun.  It explains further how
> serialization actually works and gives some guidelines how to use 
> it.
>
> <http://java.sun.com/developer/technicalArticles/Programming/serialization/>

And her is everything you'd ever want to know about serialization: 
http://java.sun.com/j2se/1.3/docs/guide/serialization/spec/serialTOC.doc.html


0
Reply mscottschilling (1976) 6/29/2008 3:16:04 AM

Mike Schilling wrote:
> Mark Space wrote:
>> zalek wrote:
>>> The  interface Serializable does not have any methods or variables.
>>> If I create a subclass of an Object class - is this class
>>> Serializable?
>>
>> Lasse gave you some really good answers to your questions.  Here's a
>> decent how-to on serialization from Sun.  It explains further how
>> serialization actually works and gives some guidelines how to use 
>> it.
>>
>> <http://java.sun.com/developer/technicalArticles/Programming/serialization/>
> 
> And her is everything you'd ever want to know about serialization: 
> http://java.sun.com/j2se/1.3/docs/guide/serialization/spec/serialTOC.doc.html

I don't think it has changed, but:
 
http://java.sun.com/javase/6/docs/platform/serialization/spec/serialTOC.html

Arne
0
Reply arne6 (9485) 6/29/2008 3:38:01 AM

Lasse Reichstein Nielsen wrote:
> Daniel Pitts <newsgroup.spamfilter@virtualinfinity.net> writes:
> 
> [Serializable]
>> Actually, It has uses as a Type, but unfortunately the original
>> *ObjectStream API developers saw fit to accept "Object" instead of
>> Serializable.
> 
> True, I have used Serializable for parameters to serialization helper
> methods.
> 
> I would prefer it as an annotation, but I also want to add annotation
> restrictions to parameters, e.g., something like:
>   void <T extends MyType & @MySerializable> doSomething(T param)
> 
> /L
I think I would prefer a (more verbose but) more explicit syntax:

void <T extends @TypeAnnotatedWith(@MySerializable) MyType> 
doSomething(T param)
but that's another story.


-- 
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
0
Reply newsgroup.spamfilter (920) 6/30/2008 3:02:01 PM

10 Replies
22 Views

(page loaded in 0.128 seconds)

Similiar Articles:













7/22/2012 12:23:22 PM


Reply: