Newbie - I don't understand interface Serializable

Z

zalek

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
 
L

Lasse Reichstein Nielsen

zalek said:
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
 
Z

zalek

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 it implements the Serializable interface.



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.




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

/L

Thanks!!!

Zalek
 
D

Daniel Pitts

Lasse said:
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.
 
L

Lasse Reichstein Nielsen

[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
 
A

Arne Vajhøj

Lasse said:
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
 
R

Roedy Green

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.
 
D

Daniel Pitts

Lasse said:
[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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top