Page 1 of 1
About Question enthuware.ocajp.i.v7.2.983 :
Posted: Wed Jan 22, 2014 4:38 am
by mmontelatici
The fifth answer is:
Code: Select all
Object o = b; Runnable r = (Runnable) b;
And the explanation is:
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 is will still work fine with the explicit cast though.
But if I try to avoid the explicit cast on NetBeans i get:
incompatible types: Object cannot be converted to Runnable
So what?
Thanks
Re: About Question enthuware.ocajp.i.v7.2.983 :
Posted: Wed Jan 22, 2014 5:39 am
by admin
Option 5 and the explanation are correct. You need to post the exact code that you have typed. There must be some difference because of which you are getting this message.
BTW, try to use command line to compile and run code while preparing for the exam. We do not recommend using IDEs for this purpose.
Re: About Question enthuware.ocajp.i.v7.2.983 :
Posted: Thu Apr 02, 2015 4:33 am
by German
Hi Pual,
could you please explain why the following option is not valid:
Object o = a; Observer ob = (Observer) o ;
while this one is valid:
Object o = a; Runnable r = (Runnable) o;
?
Re: About Question enthuware.ocajp.i.v7.2.983 :
Posted: Thu Apr 02, 2015 5:27 am
by admin
Is the reference o pointing to an object of class Observer?
Re: About Question enthuware.ocajp.i.v7.2.983 :
Posted: Thu Apr 02, 2015 9:22 am
by German
so, does it mean if it would be:
Code: Select all
Object o = b; Observer ob = (Observer) o;
then it would be valid as
o pointing to
Observer class?
Re: About Question enthuware.ocajp.i.v7.2.983 :
Posted: Thu Apr 02, 2015 9:39 am
by admin
1. "o pointing to Observer class" is an incorrect way to say it. You should say, "o pointing to an instance of Observer".
2. Yes.
Re: About Question enthuware.ocajp.i.v7.2.983 :
Posted: Mon May 18, 2015 5:25 pm
by alexandrehsantos
In this case:
The compiler doesn't know because Object is superclass for all class? So Runnable is a Object not unlike, (Object o) though now being a reference for (a) the compiler does not check the actual reference variable, only the type declared on (Object o) ?
It is correct think that way?
Re: About Question enthuware.ocajp.i.v7.2.983 :
Posted: Mon May 18, 2015 7:55 pm
by admin
Yes, that is correct. Since the compiler doesn't execute any code it can only see if it is possible for a variable to point to an object to which it is being cast. If there is a possibility, it allows the cast, otherwise, it generates an error. For example,
String s = "some string";
Runnable r = (String) s; <-- this will not compile because the compiler knows that s can never point to an object that is-a Runnable.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.983 :
Posted: Sun Sep 13, 2015 4:43 am
by Roibeard
does the explanatory text
'o' is declared as an Object. so same case as in choice 1.
mean the same thing as
'o' is declared as an Object which is Observable. so same case as in choice 1.
//for my notes. thanks.
Re: About Question enthuware.ocajp.i.v7.2.983 :
Posted: Sun Sep 13, 2015 7:45 am
by admin
Option 1 and option 4 both are trying to assign o to a reference of a different type. The declared class of o is Object while the declared type of the target is Runnable or Observable. Both are wrong for the same reason as explained in the explanation, "Although o refers to an object which is Runnable but the compiler doesn't know about it. You have to do: Runnable r = (Runnable) o;"
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.983 :
Posted: Fri Nov 13, 2015 8:40 am
by Mushfiq Mammadov
Maybe
Observable should be
Observer.

Re: About Question enthuware.ocajp.i.v7.2.983 :
Posted: Fri Nov 13, 2015 9:27 pm
by admin
Fixed.
thank you for your feedback!