• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

serialization doubt

 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

"an instance of type Computer Has-a keyboard. Because Keyboard doesnt implment Serializable, any attempt to serialize an insatnce of Computer will cause an exception to be thrown"

so if they are "IS-A" related instead, it wont throw an exception?
(as seen on other examples, sth like during de-serialization, the superclass' constructor will be run.)
 
Ranch Hand
Posts: 140
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes if "computer IS-A keyboard" you can save the computer object.

public class Computer extends Keyboard implements Serializable {.... }

the complier doesnt care if the super class of the object that you are saving is serializable or not.

when you de-serialize the computer object, keyboard values will be instantiated by its default values. only the computer object values are restored.
 
Greenhorn
Posts: 19
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But Keyboard's object is not being serialized then why there is exception?
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nikhil Arora wrote:But Keyboard's object is not being serialized then why there is exception?



Serializing an object will try to serialize all non-transient instance fields of that object (by default). And the Computer instance HAS-A Keyboard instance.

Henry
 
Nikhil Arora
Greenhorn
Posts: 19
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Nikhil Arora wrote:But Keyboard's object is not being serialized then why there is exception?



Serializing an object will try to serialize all non-transient instance fields of that object (by default). And the Computer instance HAS-A Keyboard instance.

Henry



Thanks Henry
 
I can't take it! You are too smart for me! Here is the tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic