About Question enthuware.ocpjp.v7.2.1702 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
goblingift
Posts: 6
Joined: Mon Mar 24, 2014 5:35 pm
Contact:

About Question enthuware.ocpjp.v7.2.1702 :

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

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

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

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

Alexey Berezkin
Posts: 20
Joined: Fri Jun 19, 2015 9:17 am
Contact:

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

Post 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.

Lito1900
Posts: 2
Joined: Thu Sep 10, 2015 8:38 am
Contact:

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

Post 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;

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

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

Post by admin »

But k is 5. Not 10.
If you like our products and services, please help us by posting your review here.

Lito1900
Posts: 2
Joined: Thu Sep 10, 2015 8:38 am
Contact:

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

Post by Lito1900 »

Oh it is true...
Thanks

tugrulkarakaya
Posts: 3
Joined: Mon May 20, 2019 10:20 am
Contact:

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

Post 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)

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

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

Post by admin »

Why do you think BooBoo is Serializable?
If you like our products and services, please help us by posting your review here.

andreapazzo
Posts: 1
Joined: Sat Apr 24, 2021 5:11 am
Contact:

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

Post 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

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

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

Post by admin »

Did you try running the given code?
(Hint: Observe what is being printed by Boo's constructor.)
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 29 guests