• 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

Why both GenericServlet and HttpServlet implements Serializable interface ?

 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Servlet API says that GenericServlet class implements java.io.Serializable. Then why HTTPServlet which is a subclass of GenericServlet implements that interface again?
Serializable functionality has already been fulfilled by GenericServlet class. So is it really necessary to mark the HTTPServlet class again? Please advice.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's perfectly possible for a subclass of a Serializable class not to be Serializable - by having a field that is not Serializable.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:It's perfectly possible for a subclass of a Serializable class not to be Serializable - by having a field that is not Serializable.



Incorrect. If a class implements an interface then all it's sub-classes also implement that interface.
 
Ranch Hand
Posts: 623
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Kevin Yeah, it's correct but the Serializable is just a marker interface. By explicitly implementing it, you're saying that you are aware of the serializable restrictions and your class is prepared for it.

As Ulf said - if one of your sublcasses implements the Serializable interface, it doesn't necesary mean that your class IS SERIALIZABLE.
Be aware that in the above sentences the "IS SERIALIZABLE" should be understood as the class that can be serialized, and not as the class that implements (implicitly) the Serializable interface.
 
Kevin Kilbane
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pedro Kowalski wrote:@Kevin Yeah, it's correct but the Serializable is just a marker interface. By explicitly implementing it, you're saying that you are aware of the serializable restrictions and your class is prepared for it.

As Ulf said - if one of your sublcasses implements the Serializable interface, it doesn't necesary mean that your class IS SERIALIZABLE.
Be aware that in the above sentences the "IS SERIALIZABLE" should be understood as the class that can be serialized, and not as the class that implements (implicitly) the Serializable interface.



I understand your points but I don't agree!

A class is either Serializable or not - as far as the compiler or the JVM are concerned there is no such thing as implicit or explicit implementation of an interface.

if one of your sublcasses implements the Serializable interface, it doesn't necesary mean that your class IS SERIALIZABLE.


Yes it does! If your class is not Serializable then your code is wrong. If you extend a class that implements Serializable then you need to make sure your class is Serializable (you can mark elements as transient if needs be). If you don't, your program is wrong.

 
Piotr Nowicki
Ranch Hand
Posts: 623
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Kevin, OK I understand your point of view. So, in my opinion, it's just a matter of style:

If one of my sublcasses is serializable than I must ensure that I need to be serializable too.
One can state: "it's obvious!" and the other can state: "Ok, I've taken care of that, so I'll mark it explicitly as Serializable for clarity for future developers".

There are no implicit or explicit implementations of an interface in the JVM world, but in my opinion, there is such a thing in the developers world.


Kevin Kilbane wrote:

if one of your sublcasses implements the Serializable interface, it doesn't necesary mean that your class IS SERIALIZABLE.


Yes it does! If your class is not Serializable then your code is wrong. If you extend a class that implements Serializable then you need to make sure your class is Serializable (you can mark elements as transient if needs be). If you don't, your program is wrong.



Of course, the code is poorly written, but it doesn't change the fact that your class cannot be serializabled. I believe I specifically defined in the next sentence what I mean by 'is serializable'.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic