File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes is Serialization Inherited Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "is Serialization Inherited" Watch "is Serialization Inherited" New topic
Author

is Serialization Inherited

Chetan Raju
Ranch Hand

Joined: Aug 02, 2006
Posts: 109
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.
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
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.


Rahul Bhattacharjee
LinkedIn - Blog
Ayub ali khan
Ranch Hand

Joined: Oct 20, 2005
Posts: 380
    
    2
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.


SCEA part I,TOGAF Foundation
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Moved from Servlets to Java In General (Intermediate).


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
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

Joined: Oct 30, 2001
Posts: 1970
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).
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: is Serialization Inherited