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

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

Moderator: admin

Post Reply
vchhang
Posts: 36
Joined: Tue May 06, 2014 8:30 am
Contact:

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

Post by vchhang »

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?

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

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

Post by admin »

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.
If you like our products and services, please help us by posting your review here.

Srijith
Posts: 2
Joined: Tue Aug 18, 2015 8:05 am
Contact:

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

Post by Srijith »

Hello,

Was shadowing possible if the class B doesnt extend A ?

Thanks ,
Srijith

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

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

Post by admin »

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.

ananias
Posts: 3
Joined: Fri Jun 29, 2018 1:33 am
Contact:

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

Post by ananias »

what would be the output if it was like:

System.out.println( ( (A) new B() ).fi );

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

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

Post by admin »

What did you get when you tried it out?
If you like our products and services, please help us by posting your review here.

ananias
Posts: 3
Joined: Fri Jun 29, 2018 1:33 am
Contact:

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

Post by ananias »

it printed 10, why?

I thought it would print 15 since i'm just using a new object of B.

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

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

Post by admin »

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.

herbertscbr
Posts: 7
Joined: Wed Aug 22, 2018 9:57 am
Contact:

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

Post by herbertscbr »

vchhang wrote:
Thu Jun 05, 2014 9:15 am
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?
example without casting:
A b1 = new B();
System.out.println(b1.fi );//print 10

Dreamweaver
Posts: 32
Joined: Mon Dec 29, 2014 4:14 pm
Contact:

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

Post by Dreamweaver »

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

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

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

Post by admin »

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.

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 46 guests