About Question enthuware.ocpjp.v8.2.1488 :
Moderator: admin
-
- Posts: 33
- Joined: Sat Feb 15, 2020 12:44 am
- Contact:
About Question enthuware.ocpjp.v8.2.1488 :
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.
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1488 :
Right.
If you like our products and services, please help us by posting your review here.
-
- Posts: 29
- Joined: Sun Feb 07, 2021 6:30 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1488 :
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
?
if we had:
t.j = 200;
t.k = 300;
before serialization/deserialization happened,
then output at end would be:
10 200 300
?
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1488 :
What happened when you tried it out?
If you like our products and services, please help us by posting your review here.
-
- Posts: 29
- Joined: Sun Feb 07, 2021 6:30 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1488 :
Tested and confirmed:
Output is:
10 200 300
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);
}
}
10 200 300
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1488 :
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.
Who is online
Users browsing this forum: No registered users and 54 guests