*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How many objects Serialized? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How many objects Serialized?" Watch "How many objects Serialized?" New topic
Author

How many objects Serialized?

Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
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 ]

cmbhatt
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3324
    
    8
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;
}


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
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

Joined: Sep 23, 2000
Posts: 3324
    
    8
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

Joined: Feb 28, 2007
Posts: 1707
Thanks a lot Paul,

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

for you!




Regards,
cmbhatt
 
wood burning stoves
 
subject: How many objects Serialized?