About Question enthuware.ocajp.i.v7.2.1337 :
Moderator: admin
-
- Posts: 15
- Joined: Sun Jan 28, 2018 12:38 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1337 :
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 ?
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 ?
-
- Posts: 7
- Joined: Fri May 04, 2018 4:14 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1337 :
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?
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?
-
- Site Admin
- Posts: 10043
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1337 :
Please go through the discussion above.
If you like our products and services, please help us by posting your review here.
-
- Posts: 85
- Joined: Mon Dec 24, 2018 6:24 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1337 :
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
-
- Site Admin
- Posts: 10043
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1337 :
Please go through the discussion above.
If you like our products and services, please help us by posting your review here.
-
- Posts: 38
- Joined: Thu Jan 07, 2021 2:47 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1337 :
I dont mean to be a smart-ass but since in other questions it was more precise and especially since there is a "none of the above"-option, I would suggest to remove the comma from the response options, as there is no piece of code printing any comma. The correct output would be 0 4.
Great question by the way. I would never have imagined in my wildest dreams that A calls the overridden method. I would have thought it always calls its own method. This is crazy....
Great question by the way. I would never have imagined in my wildest dreams that A calls the overridden method. I would have thought it always calls its own method. This is crazy....
-
- Site Admin
- Posts: 10043
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1337 :
Good point. Done.
thank you for your feedback!
thank you for your feedback!
If you like our products and services, please help us by posting your review here.
-
- Posts: 9
- Joined: Mon Dec 14, 2020 8:53 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1337 :
Shouldn't this be "None of the above"? The result after executing the code is 04 and not 0 4.
-
- Site Admin
- Posts: 10043
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1337 :
You are right. Updated System.out.print(i); to System.out.print(i+" ");
thank you for your feedback!
thank you for your feedback!
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 18 guests