Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

is Serialization Inherited

 
Chetan Raju
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • 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.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • 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.
 
Ayub ali khan
Ranch Hand
Posts: 394
2
  • Mark post as helpful
  • send pies
  • 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.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved from Servlets to Java In General (Intermediate).
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Mark post as helpful
  • send pies
  • 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).
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic