About Question enthuware.ocpjp.v8.2.1488 :

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

Moderator: admin

Post Reply
bvrulez
Posts: 33
Joined: Sat Feb 15, 2020 12:44 am
Contact:

About Question enthuware.ocpjp.v8.2.1488 :

Post by bvrulez »

The correct answer shows that the first print to console comes for "Booby". So this means the constructor chain is going backwards from Tooby over Dooby to Booby (and then also to Object) but is actually working through its constructors then from the most basic class back to Tooby, right? This is OCA stuff, of course, but I somehow did realize it just now.

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

Re: About Question enthuware.ocpjp.v8.2.1488 :

Post by admin »

Right.
If you like our products and services, please help us by posting your review here.

jme_chg
Posts: 29
Joined: Sun Feb 07, 2021 6:30 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1488 :

Post by jme_chg »

So just to confirm,

if we had:
t.j = 200;
t.k = 300;
before serialization/deserialization happened,
then output at end would be:
10 200 300
?

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

Re: About Question enthuware.ocpjp.v8.2.1488 :

Post by admin »

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

jme_chg
Posts: 29
Joined: Sun Feb 07, 2021 6:30 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1488 :

Post by jme_chg »

Tested and confirmed:

Code: Select all

class B { //B not Serializable so its properties cannot be serialized…
    int i; public B() { i = 10; }
}
class D extends B implements Serializable {
    int j; public D() { j = 20; }
}
class T extends D { //D implements Serializable so T also Serializable…
    int k; public T() { k = 30; }
}
public class Main {
    public static void main(String[] args) throws Exception {
        T t = new T();
        t.i = 100;
        t.j = 200;
        t.k = 300;
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("text.txt"));
        oos.writeObject(t);
        oos.close();
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("text.txt"));
        T t2 = (T) ois.readObject();
        ois.close();
        System.out.println(t2.i + " " + t2.j + " " + t2.k);
    }
}
Output is:
10 200 300

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

Re: About Question enthuware.ocpjp.v8.2.1488 :

Post by admin »

That's great! Thanks for taking time to post your program. It will be helpful to others.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 54 guests