About Question enthuware.ocpjp.v7.2.1700 :

Moderator: admin

Post Reply
SepticInsect
Posts: 20
Joined: Tue Nov 04, 2014 1:13 am
Contact:

About Question enthuware.ocpjp.v7.2.1700 :

Post by SepticInsect » Sat Dec 06, 2014 10:54 am

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();

admin
Site Admin
Posts: 8239
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Sat Dec 06, 2014 11:09 am

Yes, that is correct.
If you like our products and services, please help us by posting your review here.

ewebxml
Posts: 72
Joined: Sun Jun 30, 2013 10:04 pm
Contact:

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

Post by ewebxml » Wed Aug 12, 2015 3:57 pm

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?
_____________________________________________________________________________

admin
Site Admin
Posts: 8239
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Wed Aug 12, 2015 11:15 pm

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.
If you like our products and services, please help us by posting your review here.

wgaynor
Posts: 3
Joined: Wed Jan 13, 2016 9:49 am
Contact:

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

Post by wgaynor » Sat Jun 25, 2016 9:30 am

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.

admin
Site Admin
Posts: 8239
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Sat Jun 25, 2016 9:58 am

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.
If you like our products and services, please help us by posting your review here.

wgaynor
Posts: 3
Joined: Wed Jan 13, 2016 9:49 am
Contact:

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

Post by wgaynor » Mon Jun 27, 2016 9:13 am

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
Last edited by admin on Mon Jun 27, 2016 10:55 am, edited 1 time in total.
Reason: Please put code within [code] [/code] so that it is easier to read

admin
Site Admin
Posts: 8239
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Mon Jun 27, 2016 11:10 am

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.
If you like our products and services, please help us by posting your review here.

jagoneye
Posts: 97
Joined: Wed Dec 28, 2016 9:00 am
Contact:

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

Post by jagoneye » Fri Jan 06, 2017 8:07 am

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??

admin
Site Admin
Posts: 8239
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Fri Jan 06, 2017 8:47 am

What happened when you tried it out?
If you like our products and services, please help us by posting your review here.

jagoneye
Posts: 97
Joined: Wed Dec 28, 2016 9:00 am
Contact:

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

Post by jagoneye » Fri Jan 06, 2017 8:55 am

Didn't try it out yet. Will try it. :)

jagoneye
Posts: 97
Joined: Wed Dec 28, 2016 9:00 am
Contact:

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

Post by jagoneye » Sat Jan 07, 2017 1:33 pm

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.

admin
Site Admin
Posts: 8239
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Sat Jan 07, 2017 11:43 pm

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.
If you like our products and services, please help us by posting your review here.

jagoneye
Posts: 97
Joined: Wed Dec 28, 2016 9:00 am
Contact:

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

Post by jagoneye » Sun Jan 08, 2017 1:31 am

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. :)

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 7 guests