About Question enthuware.ocajp.i.v7.2.1291 :
Moderator: admin
-
- Posts: 36
- Joined: Tue May 06, 2014 8:30 am
- Contact:
About Question enthuware.ocajp.i.v7.2.1291 :
I understand fi in A is final but the object reference by b is still B. Why is a cast of the object B referring to A's field?
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1291 :
Access to variables depends on the class of the reference (and not on the class of the object). Casting a reference effectively changes the class of the reference used to access the variable. You need to read about variable hiding or shadowing. This link should help: http://blog.sanaulla.info/2008/06/27/sh ... mystified/
HTH,
Paul.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Tue Aug 18, 2015 8:05 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1291 :
Hello,
Was shadowing possible if the class B doesnt extend A ?
Thanks ,
Srijith
Was shadowing possible if the class B doesnt extend A ?
Thanks ,
Srijith
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1291 :
Shadowing can also happen when you have an instance member and a local method variable with the same name. Within the method, the local variable shadows the instance member. You may read more about it here: http://docs.oracle.com/javase/specs/jls ... ml#jls-6.4
If you like our products and services, please help us by posting your review here.
-
- Posts: 3
- Joined: Fri Jun 29, 2018 1:33 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1291 :
what would be the output if it was like:
System.out.println( ( (A) new B() ).fi );
System.out.println( ( (A) new B() ).fi );
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1291 :
What did you get when you tried it out?
If you like our products and services, please help us by posting your review here.
-
- Posts: 3
- Joined: Fri Jun 29, 2018 1:33 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1291 :
it printed 10, why?
I thought it would print 15 since i'm just using a new object of B.
I thought it would print 15 since i'm just using a new object of B.
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1291 :
Access to all variables (and to all static methods) always depends on the type of the variable and not on the type of the object. This is a fundamental concept in polymorphism. I would suggest you to go through a good book to learn the basics before attempting mock exams.
If you like our products and services, please help us by posting your review here.
-
- Posts: 7
- Joined: Wed Aug 22, 2018 9:57 am
- Contact:
-
- Posts: 32
- Joined: Mon Dec 29, 2014 4:14 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1291 :
I have not yet understood what makes the "final" keyword for a difference in this question.
When I delete "final" it have the same result
PS
final in class B is clear
When I delete "final" it have the same result
PS
final in class B is clear
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1291 :
It makes no difference. It is there just to confuse the candidate because final, when applied to a method, produces a different behavior.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: Bing [Bot] and 93 guests