• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

How many objects Serialized?

 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My question may be little general!

class Animal implements java.io.Serializable {}
class Dog extends Animal {}

When we serialize Dog object, How many objects will be serialized.
(I know that Dog needs not to implement the Serializable interface explicitly because its Parent does so, (Dog inherits Serializable(marker) from Animal))


Consider one more case that is "Has-A";
In that case how many objects? Please give me a small example too!!




Thanks,
cmbhatt
[ April 28, 2007: Message edited by: Chandra Bhatt ]
 
Enthuware Software Support
Posts: 4650
51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you serialize one object, that object and the objects to which it refers (i.e. the objects that the object being serialized *has*) are serialized (provided they are Serializable directly or indirectly, otherwise an exception is thrown).

So when you serialize a Dog object, only the Dog object is serialized.

But if Dog has-a Leg, then the Dog as well as the Leg(s) will be serialized. If the Leg has-a Paw then the Paw will be serialized as well

class Dog extends Animal{
Leg[] legs;
}

class Leg{
Paw paw;
}
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul,

It means even if Dog extends Animal (Dos IS-A Animal) and Animal implements
Serializable. While serializing the Dog, only Dog is serialized. Dog is full fledged Animal as well so all animal things accessible to Dog will also be serialized but because they are part of Dog object, (only Dog object will be serialized)




Thanks,
cmbhatt
 
Paul Anilprem
Enthuware Software Support
Posts: 4650
51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Chandra Bhatt:
Thanks Paul,

It means even if Dog extends Animal (Dos IS-A Animal) and Animal implements
Serializable. While serializing the Dog, only Dog is serialized. Dog is full fledged Animal as well so all animal things accessible to Dog will also be serialized but because they are part of Dog object, (only Dog object will be serialized)




Thanks,
cmbhatt



I suspected that you were getting confused between classes and objects but I wasn't sure.

Dog and Animal are two classes but when you create a Dog object, there is only one object, which is the Dog object you created. Since a Dog is-a Animal, a Dog object includes everything that is required to be an Animal (and Object, since Object is the root class of all classes). There aren't two (or three, if you count Object class as well) objects. There is only one Dog object but it contains all the Animal (and Object) stuff as well.

It's like when you buy a Car, do you get just one thing Car or do you get two things a Car and an Automoble?
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Paul,

My intentions were the same you posted last, but you made me sure regarding that.

for you!




Regards,
cmbhatt
 
We should throw him a surprise party. It will cheer him up. We can use this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic