About Question enthuware.ocajp.i.v7.2.1337 :

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

Moderator: admin

ArpRokz
Posts: 15
Joined: Sun Jan 28, 2018 12:38 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1337 :

Post by ArpRokz »

I understood this in the following way..

When we call print() from within class A's constructor it is like calling this.print(). Here "this" refers to the currently calling object. Since the actual object used was of class B therefore class B's print() is called. Now since class B's constructor requires a call to class A constructor first so i=4 is not executed till now therefore 0 is printed.

Is this explanation correct or am I wrong ?

elias86
Posts: 7
Joined: Fri May 04, 2018 4:14 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1337 :

Post by elias86 »

Why is printed 0 instead of A???

When I call the B constructor (which is a default constructor because it's not declared) java calls the super-constructor (A constructor) so it calls print method (of the B class). This method calls print() method that prints i. So if i == 4 why it prints 0?

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

Re: About Question enthuware.ocajp.i.v7.2.1337 :

Post by admin »

Please go through the discussion above.

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1337 :

Post by crazymind »

Hi, I read your explanation of this question; However, there is one thing still confuse me. According to order of initialization, the instance variable initializes before the constructor call. So before calls A(), 'i' is initialized to 4 already. Why is it 0? thanks

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

Re: About Question enthuware.ocajp.i.v7.2.1337 :

Post by admin »

Please go through the discussion above.

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests