Page 1 of 1
About Question enthuware.ocpjp.v8.2.1488 :
Posted: Thu Feb 20, 2020 11:48 am
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.
Re: About Question enthuware.ocpjp.v8.2.1488 :
Posted: Thu Feb 20, 2020 11:51 am
by admin
Right.
Re: About Question enthuware.ocpjp.v8.2.1488 :
Posted: Sun Mar 28, 2021 11:10 am
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
?
Re: About Question enthuware.ocpjp.v8.2.1488 :
Posted: Sun Mar 28, 2021 11:20 am
by admin
What happened when you tried it out?
Re: About Question enthuware.ocpjp.v8.2.1488 :
Posted: Sun Mar 28, 2021 11:31 am
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
Re: About Question enthuware.ocpjp.v8.2.1488 :
Posted: Tue Mar 30, 2021 6:01 am
by admin
That's great! Thanks for taking time to post your program. It will be helpful to others.
Re: About Question enthuware.ocpjp.v8.2.1488 :
Posted: Sun Feb 09, 2025 6:06 pm
by raphaelzintec
i tought only Wrappers are Serialized not primitives