About Question enthuware.ocpjp.v8.2.1753 :

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

Moderator: admin

Post Reply
Perelun
Posts: 5
Joined: Sat Jul 06, 2019 7:10 am
Contact:

About Question enthuware.ocpjp.v8.2.1753 :

Post by Perelun » Sat Jul 06, 2019 11:33 am

Answer n. 3 "When deserializing an object, the no-argument constructor each of its super classes that does not implement Serializable interface is invoked." is considered wrong because "The no-argument constructor of only the first non-serializable super class is invoked. This constructor may internally invoke any constructor of its super class."

It seems to me a little misleading because a constructor always invokes the super constructor. Infact, if the call to the super constructor is not explicitly done, the java compiler automatically adds a call to the no-argument super constructor. So it is not true that a constructor "may" internally invoke any constructor of its super class, because it does it always. So if the first non-serializable class constructor is invoked, then also all its super constructors will be indirectly invoked.
And so the answer number 3 seems to me correct.

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

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

Post by admin » Sat Jul 06, 2019 11:52 am

The option is talking about the no-args constructor. It is not necessary that the no-args will always be invoked.
If you like our products and services, please help us by posting your review here.

radionoise
Posts: 2
Joined: Thu Sep 19, 2019 5:19 am
Contact:

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

Post by radionoise » Thu Sep 19, 2019 5:24 am

I don't get it. Why it is not necessary that the no-args will always be invoked? Superclass constructor is explicitly called when super() is not present.

Code: Select all

class A {
    public A() {
        System.out.print("A");
    }
}

class B extends A {
    public B() {
        System.out.print("B");
    }
}

class C extends B implements Serializable {
    public C() {
        System.out.print("C");
    }
}
If you will try to deserialize C, you will get the following output: "AB" which proves that A constructor was invoked implicitly when invoking the B constructor.

Do you mean that B constructor could invoke any other A constructor with arguments (if any) resulting in no-arg A constructor will not be invoked?

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

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

Post by admin » Thu Sep 19, 2019 5:29 am

Correct. As the explanation says: The no-argument constructor of only the first non-serializable super class is invoked. This constructor may internally invoke any constructor of its super class.
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 0 guests