• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How many objects Serialized?

 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • 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 ]
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3762
10
  • Mark post as helpful
  • send pies
  • 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
  • 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
Ranch Hand
Posts: 3762
10
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic