• Post Reply Bookmark Topic Watch Topic
  • New Topic

Serial UID in base class or interface is sufficient?  RSS feed

 
sunitha nerevetla
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's say i have 3 classes extending from a base class. Is it okay if the base class has serial UID defined as anyways those 3 other classes are extending from it. Or is it compulsory to define serial UID in every class?
Same applies for interface.
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sunitha,
If you noticed the serialVersionUID field is declared "static" which means that its a class level attribute. So, the inheritance doesn't make it work for children automatically. Static fields can't be overridden.
Also, why we would want to have serialVersionUID in parent and automatically work with children? The serialversioning is associated with "version of a class file". So each java Class must have its own serialversion because it can change independently of the parent. ie. Parent.class may have been same but Child.class may change and we need to have Child's versioning....
You can probably argue that "what if we are sure that we wouldn't need Child's versioning separately? can we just put serialVersion in parent?". To me that argument would be a violation of the correct design. We should never have such a requirement.
Regards
Maulin
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!