Page 1 of 1

About Question enthuware.ocpjp.v7.2.1702 :

Posted: Mon Mar 24, 2014 5:38 pm
by goblingift
I don´t understand why the JVM cares about the missing no-arg constructor :shock: I thougt that at deserialization, no constructor will be called?

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

Posted: Mon Mar 24, 2014 8:44 pm
by admin
Construction is not called if the class implements Serializable. But if a class implements Serializable but its super class doesn't implement Serializable, then the super class's no-args constructor is called.


For more details, please see: http://docs.oracle.com/javase/7/docs/pl ... input.html
For serializable objects, the no-arg constructor for the first non-serializable supertype is run. For serializable classes, the fields are initialized to the default value appropriate for its type. Then the fields of each class are restored by calling class-specific readObject methods, or if these are not defined, by calling the defaultReadObject method. Note that field initializers and constructors are not executed for serializable classes during deserialization.

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

Posted: Fri Jun 26, 2015 1:16 pm
by Alexey Berezkin
It's interesting that even if removed Serializable from BooBoo implemented interfaces, the answer will be the same because output it emitted on constructors call, so a candidate is not tested on knowing that objects will be serialized but won't be deserialized. Maybe it will be nice to include something like println("Serialized") right after serialization and add an option with "Serialized" text to be included into an output.

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

Posted: Thu Sep 10, 2015 8:51 am
by Lito1900
I am not quite sure but wouldn't it print
In Boo K = 10 ?
As in the Boo constructor the System.out.println("In Boo K = "+k); is called before the assignation boo =k;

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

Posted: Thu Sep 10, 2015 8:37 pm
by admin
But k is 5. Not 10.

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

Posted: Fri Sep 11, 2015 4:53 am
by Lito1900
Oh it is true...
Thanks

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

Posted: Thu May 30, 2019 3:03 am
by tugrulkarakaya
In the question BooBoo is serializable so the explanation is not correct. explanation should be "Boo is not serializabale" and throws exception. (Boo is BooBoo's super class)

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

Posted: Thu May 30, 2019 3:10 am
by admin
Why do you think BooBoo is Serializable?

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

Posted: Sat Apr 24, 2021 5:24 am
by andreapazzo
Hello, I think that explanation for this question was wrong.

The question is:

Code: Select all

What will be a part of the output when the following code is compiled and run?

class Boo {
    int boo = 10;
    public Boo(int k){ System.out.println("In Boo k = "+k); boo = k;}
}

class BooBoo extends Boo {
     public BooBoo(int k ){ super(k); System.out.println("In BooBoo k = "+k); }
}
    
class Moo extends BooBoo implements Serializable {
    int moo = 10;
    public Moo(){ super(5); System.out.println("In Moo"); }
}
    
public class TestClass {
    
    public static void main(String[] args) throws Exception{
    
        var moo = new Moo();
        var fos = new FileOutputStream("c:\\temp\\moo1.ser");
        var os = new ObjectOutputStream(fos);
        os.writeObject(moo);
        os.close();
        var fis = new FileInputStream("c:\\temp\\moo1.ser");
        var is = new ObjectInputStream(fis);
        moo = (Moo) is.readObject();
        is.close();    
      }
}
Possible solutions are:

Code: Select all

1. In Boo k = 5
    In BooBoo k = 5 
2. In Boo k = 0
    In BooBoo k = 0 
3. In Moo
4. It will throw an exception at runtime.
5. It will not compile.
The explanation

Code: Select all

While instantiating a new Moo, all the constructors will be executed. So it  will print
 In Boo k = 5
 In BooBoo k = 5
 In Moo
There will not be any problem while serializing the Moo object.
However, while deserializing, the JVM will not find any no-arg constructor that can be invoked to initialize BooBoo.
No-args constructor is required in BooBoo because BooBoo is the most specific class in the class hierarchy that doesn't implement Serializable.
So it will throw an InvalidClassException.
When Moo is initialized

Code: Select all

var moo = new Moo()
in console will be print:

Code: Select all

 In Boo k = 10
 In BooBoo k = 5
because in contructor of Boo, System.out is before k assignment.
This is the error.
Other part of explanation is ok.

Andrea

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

Posted: Sat Apr 24, 2021 6:17 am
by admin
Did you try running the given code?
(Hint: Observe what is being printed by Boo's constructor.)