• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Seralization question

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there, I was trying to confirm some of the aspects pointed out in the book related to Seralization.. I coded this example myself..
package chapter7.files;

import java.io.*;


class Animal {

protected String category;

protected transient String element = "Earth";

private int number = 2;

protected int d = 4;

public int getNumber(){return number;}

}

class Dog extends Animal implements Serializable{

String breed;

String name;

int collar_size;

public String toString(){

System.out.println("Dog class");

System.out.println("breed:" + breed);

System.out.println("name:" + name);

System.out.println("collar size:" + collar_size);

System.out.println("Category:" + category);

System.out.println("Element:" + element);

System.out.println("number:" + getNumber());

System.out.println("int d:" + d);

return "";

}


}

public class SeriaTest {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Dog d = new Dog();

d.breed = "Schanuzer";

d.name = "Lea";

d.collar_size = 2;

d.category = "mammal";

d.d = 10;

try{

FileOutputStream fo = new FileOutputStream("file.ser");

ObjectOutputStream os = new ObjectOutputStream(fo);

os.writeObject(d);

FileInputStream fi = new FileInputStream("file.ser");

ObjectInputStream oi = new ObjectInputStream(fi);

Dog d2 = (Dog)oi.readObject();

d2.toString();

}
catch(Exception ex){}
}

}

When I run this, and I have run it several times the output is:

Dog class
breed:Schanuzer
name:Lea
collar size:2
Category:null
Element:Earth
number:2
int d:4

Which it's good except for the part that the transient member attribute from class Animal is being serialized and the category attribute member is not!... this is some weird stuff, the integer d although it was changed it was serialized and reconstructed with the value which was initialized, can somebody explain why this is happening?.. thanks.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Serch Hdez,

in your example the Animal class was not serialized at all. thats because only the dog class implements the Serializable interface so the field's of that particular class is stored in the file. rest is ignored(Animal Class because it doesn't implement the Serializable interface). when the class in reconstructed the initial values are assigned to the animal class fields.

The superclasses of your serialized classes need not be Serializable. However, those superclass fields won�t be saved/restored. The fields will be restored to whatever you would get running the non-arg constructor.

Regards,

Mifraz
[ May 12, 2008: Message edited by: Abdul Mifraz ]
 
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Serch,

Always try to use code tags. which could be easier for other ranchers to understand your code.

Anyways for the convenience for other ranchers.

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How come elemnt, category and number are stored these variables are not serialised and not used by dog.
As you have meentioned that the variables which belong to dog are stored and rest are ignored which is of animal class.

Can you please liitle elaborate and explain me in simple terms.

please

thanks
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankyou Serch for the example.

Dinesh, please read "How inheritance affects serialization" topic in Chapter 6 of K&B book.

The post by Abdul Mifraz amply explains the example.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic