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 ?
About Question enthuware.ocajp.i.v7.2.1337 :
Moderator: admin
-
- Posts: 15
- Joined: Sun Jan 28, 2018 12:38 pm
- Contact:
-
- 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: 10386
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1337 :
Please go through the discussion above.
-
- 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: 10386
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1337 :
Please go through the discussion above.
Who is online
Users browsing this forum: No registered users and 8 guests