Page 1 of 1

About Question enthuware.ocpjp.v7.2.1700 :

Posted: Sat Dec 06, 2014 10:54 am
by SepticInsect
During deserialization, the constructor of the class (or any static or instance blocks) is not executed.
Be "static blocks" do you mean "Static Initialization Blocks"?

Aren't "Static Initialization Blocks" a part of class initialization? Ah, now I think I understand what you mean. In the code below "Static Initialization Blocks" whould be executed if the class Moo is used for the first time but it whould not be executed as a part of the deserialization. Is that correct?

Code: Select all

FileInputStream fis = new FileInputStream("c:\\temp\\moo1.ser");
ObjectInputStream is = new ObjectInputStream(fis);
Moo moo = (Moo) is.readObject();

Re: About Question enthuware.ocpjp.v7.2.1700 :

Posted: Sat Dec 06, 2014 11:09 am
by admin
Yes, that is correct.

Re: About Question enthuware.ocpjp.v7.2.1700 :

Posted: Wed Aug 12, 2015 3:57 pm
by ewebxml
If [the super class does not implement Serializable]
THEN[its constructor is called]
BooBoo and Boo are not Serializable so their constructor is invoked.

The parent class of Moo is BooBoo
and the parent class of BooBoo is Boo.

BooBoo and Boo do not implement Serializable

Code: Select all

class Boo {
  public Boo(){ System.out.println("In Boo"); }
}
class BooBoo extends Boo {
  public BooBoo(){ System.out.println("In BooBoo"); }
}
class Moo extends BooBoo implements Serializable {
  int moo = 10; { System.out.println("moo set to 10"); }
  public Moo(){ System.out.println("In Moo"); }
}
Question1:If BooBoo and Boo were Serializable, would their constructors be invoked?

Question2: Under which conditions are the constructors of the parent classes serialized?
_____________________________________________________________________________

Re: About Question enthuware.ocpjp.v7.2.1700 :

Posted: Wed Aug 12, 2015 11:15 pm
by admin
1. No.
2. That's the only condition. If you deserialize an object of a class, the constructor of its most specific superclass that does not implement Serializable will be invoked (which in turn will cause the constructor of its superclass to be invoked due to super()/super(params)).

You should actually try it out. Here is a sample code that you can play with:

Code: Select all

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class A implements Serializable{
    public A(){
        System.out.println("In A()");
    }
    public A(int i){
        System.out.println("In A(int)");
    }
}
class AA extends A{
    public AA(){System.out.println("In AA()");}
    public AA(int i){
        System.out.println("In AA(int)");
    }
}

public class ConstructorTest extends AA {
    public ConstructorTest(){
        super(10);
        System.out.println("In CT");
    }
    public static void main(String[] args) throws Exception {
        ConstructorTest ct = new ConstructorTest();
        FileOutputStream fos = new FileOutputStream("c:\\temp\\test2.ser");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(ct);
        oos.flush(); oos.close();fos.close();
  System.out.println("-----------------");
        FileInputStream fis = new FileInputStream("c:\\temp\\test2.ser");
        ObjectInputStream ois = new ObjectInputStream(fis);
        Object obj = ois.readObject();
        System.out.println(obj);
        
        
    }
}
HTH,
Paul.

Re: About Question enthuware.ocpjp.v7.2.1700 :

Posted: Sat Jun 25, 2016 9:30 am
by wgaynor
If the base class, Boo implements Serializable but BooBoo does not. When you de-serialize Moo, BooBoo's no-args constructor is called. BooBoo's constructor will first make a call to super(), but because Boo implements Serializable, it just returns automatically without finishing its own constructor? Why wouldn't BooBoo complete its own constructor? Hopefully this question makes sense.

Re: About Question enthuware.ocpjp.v7.2.1700 :

Posted: Sat Jun 25, 2016 9:58 am
by admin
Can you put your question in code so that it is clear as to what you are asking? Also, please run it and show the output.
-Paul.

Re: About Question enthuware.ocpjp.v7.2.1700 :

Posted: Mon Jun 27, 2016 9:13 am
by wgaynor

Code: Select all

class Boo implements Serializable{
    public Boo() {
        System.out.println("In Boo");
    }
}
class BooBoo extends Boo {
    public BooBoo() {
        System.out.println("In BooBoo");
    }
}
class Moo extends BooBoo implements Serializable {
    int moo = 10;
    {
        System.out.println("moo set to 10");
    }
    public Moo() {
        System.out.println("in Moo");
    }
}

public class TestClass {
    
    public static void main (String[] args) throws Exception {
        
//        Moo moo = new Moo();
//        moo.moo = 20;
//        FileOutputStream fos = new FileOutputStream("C:\\Users\\wgaynor\\testone\\moo1.ser");
//        ObjectOutputStream os = new ObjectOutputStream(fos);
//        os.writeObject( moo);
//        os.close();
        FileInputStream fis = new FileInputStream("C:\\Users\\wgaynor\\testone\\moo1.ser");
        ObjectInputStream os = new ObjectInputStream(fis);        
        Moo moo = (Moo) os.readObject();
        os.close();
        System.out.println(moo.moo);
    }    
}
Output: 20

So the only difference between this code and the original code in the question is that Boo now implements serializable.
I understand that because Boo now implements serializable, its no-args constructor is not called, so "In Boo" will not be in the output. But why doesn't BooBoo's no-args constructor still get called, adding "In BooBoo" to the output?

Thanks

Re: About Question enthuware.ocpjp.v7.2.1700 :

Posted: Mon Jun 27, 2016 11:10 am
by admin
Remember that all subtypes of a serializable class are themselves serializable. So if you make Boo implement Serializable, none of the constructors of BooBoo or Moo will be called irrespective of whether they explicitly implement Serializable or not.
HTH,
Paul.

Re: About Question enthuware.ocpjp.v7.2.1700 :

Posted: Fri Jan 06, 2017 8:07 am
by jagoneye
admin wrote:Remember that all subtypes of a serializable class are themselves serializable. So if you make Boo implement Serializable, none of the constructors of BooBoo or Moo will be called irrespective of whether they explicitly implement Serializable or not.
HTH,
Paul.
But what if BooBoo implements Serializable but Boo does not in our question?
Will Boo's constructor be called during deserialization of moo??

Re: About Question enthuware.ocpjp.v7.2.1700 :

Posted: Fri Jan 06, 2017 8:47 am
by admin
What happened when you tried it out?

Re: About Question enthuware.ocpjp.v7.2.1700 :

Posted: Fri Jan 06, 2017 8:55 am
by jagoneye
Didn't try it out yet. Will try it. :)

Re: About Question enthuware.ocpjp.v7.2.1700 :

Posted: Sat Jan 07, 2017 1:33 pm
by jagoneye
admin wrote:What happened when you tried it out?
Found the result to be suprising:

Code: Select all

class Boo 
{
    static
    {
        System.out.println("Static");
    }
    {
        System.out.println("Initialize Boo");
    }
    public Boo(){ System.out.println("In Boo"); }
}
class BooBoo extends Boo implements Serializable
{
    public BooBoo(){ System.out.println("In BooBoo"); }
}

class Moo extends BooBoo implements Serializable {
    int moo = 10; { System.out.println("moo set to 10"); }
    public Moo(){ System.out.println("In Moo"); }
}

    
public class UltimateSerialize { 
    
    public static void main(String[] args) throws Exception{
    
    Moo moo = new Moo();
    FileOutputStream fos = new FileOutputStream("f:\\test\\moo1.ser");
    ObjectOutputStream os = new ObjectOutputStream(fos);
    os.writeObject(moo);
    os.close();
    FileInputStream fis = new FileInputStream("f:\\test\\moo1.ser");
    ObjectInputStream is = new ObjectInputStream(fis);
    moo = (Moo) is.readObject();
    is.close();    
      }
}
Output is:

Code: Select all

Static
Initialize Boo
In Boo
In BooBoo
moo set to 10
In Moo
Initialize Boo
In Boo
I put those static and non-static initialization blocks for checking if those are called or not and they are. Surprisingly Boo class constructs are called and
Boo Boo is not! If I make Boo implement Serializable then the obvious happens
i.e. no constructs are called because of Inheritance all classes will implement
Serializable.

Re: About Question enthuware.ocpjp.v7.2.1700 :

Posted: Sat Jan 07, 2017 11:43 pm
by admin
Boo's constructor is called because it does not implement Serializable. BooBoo and Moo implement Serializable (directly or indirectly) and that is why their constructors are not called during deserialization.

HTH,
Paul.

Re: About Question enthuware.ocpjp.v7.2.1700 :

Posted: Sun Jan 08, 2017 1:31 am
by jagoneye
admin wrote:Boo's constructor is called because it does not implement Serializable. BooBoo and Moo implement Serializable (directly or indirectly) and that is why their constructors are not called during deserialization.

HTH,
Paul.
Yup cleared this concept fully. Now I am revising all the questions. Thinking of giving the exam this week. :)