Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Sub class Serialization

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Sub class Serialization

    Hi All,

    I am new to this community, First of all I would like to say Hellooooooo to every body. Now lets get back to the business .

    So my First question is around the Serialization. Let's say there is Class B which is a Sub Class of A, Now if A does not implements Serializable interface and B does, So will I be able to Serialize B ?


  2. #2
    Member
    Join Date
    Apr 2010
    Location
    The Hague, Netherlands
    Posts
    91
    Thanks
    3
    Thanked 10 Times in 10 Posts

    Default Re: Sub class Serialization

    Hi Param,

    Welcome to the forum.

    Back to youre question: ofcourse, B extends A, wich means also the interface.

  3. #3
    Junior Member
    Join Date
    Apr 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Sub class Serialization

    Quote Originally Posted by Bryan View Post
    Hi Param,

    Welcome to the forum.

    Back to youre question: ofcourse, B extends A, wich means also the interface.
    Thanks for your reply Bryan..

    I will really appriciate if could you please explain...

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Sub class Serialization

    Interfaces get inherited, but they do not propagate upwards.

    So, in this case A is not serializable, but B is.

    If you were to declare A to implement Serializable, B would automatically inherit this trait from A and also be Serializable.

  5. #5
    Junior Member
    Join Date
    Apr 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Re: Sub class Serialization

    Quote Originally Posted by helloworld922 View Post
    Interfaces get inherited, but they do not propagate upwards.

    So, in this case A is not serializable, but B is.

    If you were to declare A to implement Serializable, B would automatically inherit this trait from A and also be Serializable.

    Hey HelloWorld thank you for jumping in...

    So you mean to say that( correct me If I am wrong somewhere) in my case if class A has attribute say 'age'
    And class B has attribute say 'name' and now when I will Serilize the object of B


    1. I m not going to receive any type of compile or runtime error.
    2.Only the value of 'name' attribute of Class B will be stored , and the value of 'age'(Attribute of A class) will not be stored.

    Please confirm...

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Sub class Serialization

    edit:

    hmm.. never mind. I did some actual testing and I guess that in order to be Serializable, a class and all of it's super classes must be Serializable (except for Object, which can be written out no problem).

    However, declaring A to be Serializable as mentioned before does allow B to be Serializable (again, it's important to make sure that all members of B are Serializable).

    You can of course write your own custom serialization/deserialization code, but I think the checking for Serializable is done before the over-written methods can be called.
    Last edited by helloworld922; April 23rd, 2010 at 01:28 AM.

  7. #7
    Junior Member
    Join Date
    Apr 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Sub class Serialization

    Quote Originally Posted by helloworld922 View Post
    edit:

    hmm.. never mind. I did some actual testing and I guess that in order to be Serializable, a class and all of it's super classes must be Serializable (except for Object, which can be written out no problem).

    However, declaring A to be Serializable as mentioned before does allow B to be Serializable (again, it's important to make sure that all members of B are Serializable).

    You can of course write your own custom serialization/deserialization code, but I think the checking for Serializable is done before the over-written methods can be called.
    yeah, I did a small practical of serialization and found that attribute belongs to A(in our case age) will not be stored(Serillized) as A does not implements Serilizable interface but fileds of B will be.

    Here I am sending you the code...

    public class SerilizationExample {
    public static void main(String [] args)
    {
    Employee e = new Employee();
    e.setAge(24);
    //e.age = 100;
    e.name = "Param.....";
    e.address = "Mumbai, India....";
    e.SSN = 11122333;
    e.number = 101;
    try
    {
    FileOutputStream fileOut =
    new FileOutputStream("employee.ser");
    ObjectOutputStream out =
    new ObjectOutputStream(fileOut);
    out.writeObject(e);
    out.close();
    fileOut.close();
    }catch(IOException i)
    {
    i.printStackTrace();
    }
    }

    }

    class Human
    {
    public int age;
    public int getAge() {
    return age;
    }

    public void setAge(int age) {
    this.age = age;
    }
    }

    class Employee extends Human implements java.io.Serializable
    {
    public String name;
    public String address;
    public transient int SSN;
    public int number;
    public void mailCheck()
    {
    System.out.println("Mailing a check to " + name
    + " " + address);
    }
    }

    class DeserializeDemo
    {
    public static void main(String [] args)
    {
    Employee e = null;
    try
    {
    FileInputStream fileIn =
    new FileInputStream("employee.ser");
    ObjectInputStream in = new ObjectInputStream(fileIn);
    e = (Employee) in.readObject();
    in.close();
    fileIn.close();
    }catch(IOException i)
    {
    i.printStackTrace();
    return;
    }catch(ClassNotFoundException c)
    {
    System.out.println("Employee class not found");
    c.printStackTrace();
    return;
    }
    System.out.println("Deserialized Employee...");
    System.out.println("Name: " + e.name);
    System.out.println("Age: " + e.getAge());
    System.out.println("Address: " + e.address);
    System.out.println("SSN: " + e.SSN);
    System.out.println("Number: " + e.number);
    e.mailCheck();
    }
    }

Similar Threads

  1. Replies: 0
    Last Post: April 11th, 2010, 08:56 AM
  2. problem with data access when a class call another class
    By ea09530 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 4th, 2010, 05:20 PM
  3. Exception and Serialization Problems
    By chrisych in forum Exceptions
    Replies: 0
    Last Post: February 7th, 2010, 11:30 AM
  4. serialization problem
    By Park in forum Object Oriented Programming
    Replies: 2
    Last Post: November 26th, 2009, 04:44 PM
  5. how to load a class and type cast to that class?
    By chinni in forum Object Oriented Programming
    Replies: 2
    Last Post: November 9th, 2009, 10:18 AM