Page 1 of 1

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

Posted: Sat Apr 12, 2014 1:17 pm
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

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

Posted: Sat Apr 12, 2014 7:38 pm
by admin
Let me ask you a question. Can variable o refer to an object that doesn't implement Runnable?

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

Posted: Sun Apr 13, 2014 1:06 pm
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?

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

Posted: Sun Apr 13, 2014 7:51 pm
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.

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

Posted: Thu Aug 18, 2016 6:14 am
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.

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

Posted: Thu Aug 18, 2016 8:50 am
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?

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

Posted: Fri Aug 19, 2016 4:07 am
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.

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

Posted: Fri Oct 20, 2017 12:17 pm
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?

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

Posted: Sat Oct 21, 2017 11:56 am
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.

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

Posted: Thu Jan 04, 2018 10:29 am
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"

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

Posted: Sun Sep 02, 2018 8:12 am
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.

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

Posted: Sun Sep 02, 2018 9:06 am
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.

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

Posted: Mon Jul 29, 2019 11:49 am
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?

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

Posted: Mon Jul 29, 2019 12:42 pm
by admin
Yes, your class diagram is correct.