Results 1 to 6 of 6
  1. #1
    vysh is offline Member
    Join Date
    Apr 2009
    Posts
    11
    Rep Power
    0

    Default [SOLVED] controversy about the required answer

    Hi all,

    I am a new member to this forum. Kindly apologize if i have posted my question in the wrong forum and guide me where to post it.

    Coming to my question: I saw in one of the SCJP dumps about a question and executed it. I got a different answer but the answer given in the dump is different. Can anyone tell me which one is correct?

    My code is as follows:

    Java Code:
    import java.io.*;
    class Food {
    Food() 
    { 
    System.out.print("1"); 
    }
    }
    class Fruit extends Food implements Serializable
    {
    Fruit() { System.out.print("2");
    }
    }
    public class Banana2 extends Fruit { 
    int size = 42;
    Banana2 serializeBanana2(Banana2 b)
    {
    return b;
    }
    Banana2 deserializeBanana2(Banana2 b)
    {
    return b;
    }
    public static void main(String [] args)
    {
    Banana2 b = new Banana2();
    b.serializeBanana2(b);
    b = b.deserializeBanana2(b);
    System.out.println(" restored " + b.size + " "); 
    }
    }
    I got the output as "12 restored 42". But the answer which is given in the dump is "121 restored 42". Can anyone help me clear this controversy?

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Um, you are aware that's not serialization? You need ObjectOutputStream and ObjectInputStream. The methods to override are called readObject(ObjectInputStream) and writeObject(ObjectInputStream).

    As for the question, this code will always print "12 restored 42". If you used actual serialization, it may do something else.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    vysh is offline Member
    Join Date
    Apr 2009
    Posts
    11
    Rep Power
    0

    Default

    Ya thats right. I havent used the actual serialization, the serialized part which i have given is my own code. I will post the original code given in the dump here:

    Java Code:
    import java.io.*;
    class Food 
    {
    Food() 
    { 
    System.out.print("1"); 
    } 
    }
    class Fruit extends Food implements Serializable {
    Fruit()
    { 
    System.out.print("2"); 
    } 
    }
    public class Banana2 extends Fruit
    { 
    int size = 42;
    public static void main(String [] args) {
    Banana2 b = new Banana2();
    b.serializeBanana2(b); // assume correct serialization
    b = b.deserializeBanana2(b); // assume correct
    System.out.println(" restored " + b.size + " "); 
    }
    // more Banana2 methods
    }
    With this code, how could the output be given as '121 restored 42'? Can you please help me?

  4. #4
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    835
    Rep Power
    6

    Default

    The first code what u have given will print 12 restored 42 and recent what u have posted will throw compilation error because no methods what u have mentioned

  5. #5
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    When you deserialize objects, their default constructors are called, then their fields are re-set. See Java Object Serialization Specification: Contents for the full details.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  6. #6
    vysh is offline Member
    Join Date
    Apr 2009
    Posts
    11
    Rep Power
    0

    Default

    Interesting. So when I deserialize the object, my Food() constructor is called automatically. To add to your point, the Fruit() constructor is not called, because it implements Serializable.

    Thank you very much.

    Ramya,

    The second code, is a code given in the dump. They dont give exact codes, they give only precise codes.

Similar Threads

  1. Why is the answer not coming out
    By anonymous18 in forum New To Java
    Replies: 4
    Last Post: 11-12-2008, 03:10 AM
  2. Replies: 3
    Last Post: 11-02-2008, 01:29 AM
  3. Plz answer this question ...
    By raghu2114 in forum Advanced Java
    Replies: 2
    Last Post: 09-19-2008, 06:36 PM
  4. guss which is correct answer
    By abhinav_jain09 in forum Java Applets
    Replies: 4
    Last Post: 09-19-2008, 02:39 AM
  5. What is the answer yo my problem with this code
    By masaka in forum New To Java
    Replies: 4
    Last Post: 03-26-2008, 06:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •