Newbie - I don't understand interface Serializable

Discussion in 'Java' started by zalek, Jun 28, 2008.

  1. zalek

    zalek Guest

    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
    zalek, Jun 28, 2008
    #1
    1. Advertising

  2. zalek <> 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.'
    Lasse Reichstein Nielsen, Jun 28, 2008
    #2
    1. Advertising

  3. zalek

    zalek Guest

    On Jun 28, 12:32 pm, Lasse Reichstein Nielsen <> wrote:
    > zalek <> 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
    zalek, Jun 28, 2008
    #3
  4. zalek

    Daniel Pitts Guest

    Lasse Reichstein Nielsen wrote:
    > zalek <> 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/>
    Daniel Pitts, Jun 28, 2008
    #4
  5. Daniel Pitts <> 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.'
    Lasse Reichstein Nielsen, Jun 28, 2008
    #5
  6. zalek

    Mark Space Guest

    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/>
    Mark Space, Jun 28, 2008
    #6
  7. zalek

    Arne Vajhøj Guest

    Lasse Reichstein Nielsen wrote:
    > Daniel Pitts <> 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
    Arne Vajhøj, Jun 28, 2008
    #7
  8. zalek

    Roedy Green Guest

    On Sat, 28 Jun 2008 09:23:07 -0700 (PDT), zalek
    <> 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
    Roedy Green, Jun 29, 2008
    #8
  9. 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
    Mike Schilling, Jun 29, 2008
    #9
  10. zalek

    Arne Vajhøj Guest

    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
    Arne Vajhøj, Jun 29, 2008
    #10
  11. zalek

    Daniel Pitts Guest

    Lasse Reichstein Nielsen wrote:
    > Daniel Pitts <> 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/>
    Daniel Pitts, Jun 30, 2008
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve Line
    Replies:
    8
    Views:
    656
    Tony Morris
    Jan 14, 2004
  2. Replies:
    2
    Views:
    818
    placid
    Jan 9, 2006
  3. Thomas Hawtin
    Replies:
    2
    Views:
    6,088
  4. Jimmy
    Replies:
    4
    Views:
    631
    Thomas Hawtin
    Aug 8, 2007
  5. Replies:
    8
    Views:
    2,861
Loading...

Share This Page