Clarification request on question ID enthuware.ocajp.i.v7.2.983

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

Moderator: admin

Post Reply
aarulselvam
Posts: 2
Joined: Sat Apr 12, 2014 1:07 pm
Contact:

Clarification request on question ID enthuware.ocajp.i.v7.2.983

Post by aarulselvam »

Option 2 says:

Code: Select all

Object o = a; Runnable r = (Runnable) o;
Here you are explicitly telling the compiler that o refers to an object that is Runnable.

Option 5 says:

Code: Select all

Object o = b; Runnable r = (Runnable) b;
Since b is declared of a type that indirectly implements Runnable, the compiler can figure out that b will always point to an object that is assignable to a Runnable. Therefore, explicit cast is not required here. It will still work fine with the explicit cast though.

My Confusion:

While Option 5 says that the compiler can figure out that b will always point to an object of Runnable. I was wondering why not the same applicable for Option 2? Why does Option 2 require explicit casting?

I am not an experienced JAVA developer and trying to learn. May be I am missing something here. Appreciate if any one can assist me to understand.

With regards,
Arul

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

Re: Clarification request on question ID enthuware.ocajp.i.v7.2.983

Post by admin »

Let me ask you a question. Can variable o refer to an object that doesn't implement Runnable?
If you like our products and services, please help us by posting your review here.

aarulselvam
Posts: 2
Joined: Sat Apr 12, 2014 1:07 pm
Contact:

Re: Clarification request on question ID enthuware.ocajp.i.v7.2.983

Post by aarulselvam »

In the question, variable o refer to the object that implement Runnable. Let me reproduce the question here:

Consider the following classes:

class A implements Runnable{ ...}
class B extends A implements Observer { ...}

(Assume that Observer has no relation to Runnable.) and the declarations :   

A a = new A() ;   
B b = new B();

Which of the following Java code fragments will compile and execute without throwing exceptions?

Option 2:

Code: Select all

Object o = a; Runnable r = (Runnable) o;
Option 5:

Code: Select all

Object o = b; Runnable r = (Runnable) b;
Here variable o refer to an object a which implement Runnable.

My query is that casting (Runnable) required for option 2?

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

Re: Clarification request on question ID enthuware.ocajp.i.v7.2.983

Post by admin »

Yes, it is required. I asked you the question to make it easy for you to learn. But anyway, cast is required because it is possible for o to point to any object so the compiler needs to be assured that it will point to a Runnable at run time.
If you like our products and services, please help us by posting your review here.

maxmurks
Posts: 2
Joined: Thu Aug 18, 2016 6:12 am
Contact:

Re: Clarification request on question ID enthuware.ocajp.i.v

Post by maxmurks »

The thing is: The explanation for Option 5 says, that the explicit cast is not required in option 5. Which I'm quite sure is wrong.

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

Re: Clarification request on question ID enthuware.ocajp.i.v

Post by admin »

maxmurks wrote:The thing is: The explanation for Option 5 says, that the explicit cast is not required in option 5. Which I'm quite sure is wrong.
Did you try compiling the code?
If you like our products and services, please help us by posting your review here.

maxmurks
Posts: 2
Joined: Thu Aug 18, 2016 6:12 am
Contact:

Re: Clarification request on question ID enthuware.ocajp.i.v

Post by maxmurks »

Yes, I tried compiling it. Turns out I should try reading the options correctly first.

I read (and therefore typed) Option 5 like

Object o = b; Runnable r = (Runnable) o;

instead of

Object o = b; Runnable r = (Runnable) b;

So kind of exactly like Option2.

Sorry about that.

Sergey
Posts: 39
Joined: Sat Jul 29, 2017 1:04 pm
Contact:

Re: Clarification request on question ID enthuware.ocajp.i.v

Post by Sergey »

Although, at run time, o does point to a Runnable, the compiler doesn't know about it
how could we know that o does point to a Runnable?

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

Re: Clarification request on question ID enthuware.ocajp.i.v

Post by admin »

Sergey wrote:
Although, at run time, o does point to a Runnable, the compiler doesn't know about it
how could we know that o does point to a Runnable?
Because of the statement Object o = a;
and the fact that a points to an object of class A, which implements Runnable.
If you like our products and services, please help us by posting your review here.

Sergey
Posts: 39
Joined: Sat Jul 29, 2017 1:04 pm
Contact:

Re: Clarification request on question ID enthuware.ocajp.i.v

Post by Sergey »

maxmurks wrote:Yes, I tried compiling it. Turns out I should try reading the options correctly first.

I read (and therefore typed) Option 5 like

Object o = b; Runnable r = (Runnable) o;

instead of

Object o = b; Runnable r = (Runnable) b;

So kind of exactly like Option2.

Sorry about that.
I did the same mistake. But i think, the question should be "very hard" rather than "easy"

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

Re: Clarification request on question ID enthuware.ocajp.i.v7.2.983

Post by flex567 »

Currently answer 4 is phrased like this

Code: Select all

Object o = b; Runnable r = (Runnable) b;
Could answer 4 be phrased like this:

Code: Select all

Runnable r = (Runnable) b;
Because unlike previous answers these 2 statements are not related.

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

Re: Clarification request on question ID enthuware.ocajp.i.v7.2.983

Post by admin »

No, the option is fine as it is. You may see options that don't make much sense in practice but that test your Java knowledge nevertheless.
If you like our products and services, please help us by posting your review here.

Mikhail Volkov
Posts: 8
Joined: Mon Jul 29, 2019 11:25 am
Contact:

Re: Clarification request on question ID enthuware.ocajp.i.v7.2.983

Post by Mikhail Volkov »

Hello! Please, tell me - did I paint correct schema for to solve this question? http://joxi.ru/52aEWXTEa8G720
I think, that when I have correct schema it is easier to find correct answer.
But I'm not sure...

In the fact I find very strange - make Interface as Type of reference... For Why?
Attachments
e271eb3ab8.png
e271eb3ab8.png (10.03 KiB) Viewed 3589 times

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

Re: Clarification request on question ID enthuware.ocajp.i.v7.2.983

Post by admin »

Yes, your class diagram is correct.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 46 guests