• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

is Serialization Inherited

 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My question is if the superclass implements the Serializable interface, then is the subclass also serializable by default ?

If yes, then why does HttpServlet implement Serializable interface when the superclass that it extends, GenericServlet, has already implemented Serializable interface ?

I hope I am clear with my question.
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes , If the super class implements serializable , then no need that the sub classes would also be implementing serializable interface.

Why is has been done in that way I do not know , but its for sure not necessary.
 
Ranch Hand
Posts: 399
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

If the super class implements serialiable interface, its a best practice to declare in the subclass that it also implements the same interface. This way if some one just reads the subclass he will have the complete picture.

This is more of a best practice, not a compulsion.

Best regards
Ayub.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moved from Servlets to Java In General (Intermediate).
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rahul Bhattacharjee:
Why is has been done in that way I do not know



Surely, it's to make the feature reasonably object-oriented (OO).

Say you have class A that is Serializable, and class B that is a subclass of A. Good OO practice requires that you can treat an object of class B as an object of class A. Class B can have additional features, or override some of class A's features, but should not omit any of class B's features. So, if A is Serializable, then class B should be, too.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Peter Chase:


Surely, it's to make the feature reasonably object-oriented (OO).

So, if A is Serializable, then class B should be, too.



That all true, but if this automatically happens then whats the need to explictly mark that by implementing serializable.
 
Peter Chase
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rahul Bhattacharjee:
whats the need to explictly mark that by implementing serializable.



Wuh?

In my example, superclass A has to be declared Serializable, otherwise neither A nor B will be able to be serialised. Subclass B inherits Serializable from A, so it does not need to be declared Serializable. However, some people might like explicitly to declare class B as Serializable, purely to make the code more self-documenting (personally, I wouldn't bother, but I can see those people have a case).
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It *might* also be for historical reasons - perhaps at first only the subclass was serializable, and the developer who later made the superclass serializable didn't care to remove the declaration from all subclasses.
 
Bring out your dead! Or a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic