wood burning stoves*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Why both GenericServlet and HttpServlet implements Serializable interface ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Why both GenericServlet and HttpServlet implements Serializable interface ?" Watch "Why both GenericServlet and HttpServlet implements Serializable interface ?" New topic
Author

Why both GenericServlet and HttpServlet implements Serializable interface ?

Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
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.


OCPJP 6.0-81% | Preparing for OCWCD
http://www.certpal.com/blogs/cert-articles | http://sites.google.com/site/mostlyjava/scwcd |
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41137
    
  45
It's perfectly possible for a subclass of a Serializable class not to be Serializable - by having a field that is not Serializable.


Ping & DNS - my free Android networking tools app
Kevin Kilbane
Ranch Hand

Joined: Sep 22, 2008
Posts: 42
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.
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

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


OCP Java SE 6 Programmer, OCM Java SE 6 Developer, OCE Java EE 6 JSPSD, OCE Java EE 6 EJBD, OCE Java EE 6 JPAD, Spring 3.0 Core Professional.
Kevin Kilbane
Ranch Hand

Joined: Sep 22, 2008
Posts: 42
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

Joined: Jul 13, 2010
Posts: 610

@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'.
 
Don't get me started about those stupid light bulbs.
 
subject: Why both GenericServlet and HttpServlet implements Serializable interface ?
 
Similar Threads
Why HttpServlet implements Serializable interface
GenericServlet Vs HttpServlet
need advice
Why HttpServlet also implements Serializable?
why java.io.Serializable again?