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

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
mmontelatici
Posts: 2
Joined: Wed Jan 22, 2014 4:24 am
Contact:

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

Post 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

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

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

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

German
Posts: 5
Joined: Thu Apr 02, 2015 4:28 am
Contact:

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

Post 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;
?

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

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

Post by admin »

Is the reference o pointing to an object of class Observer?
If you like our products and services, please help us by posting your review here.

German
Posts: 5
Joined: Thu Apr 02, 2015 4:28 am
Contact:

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

Post 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?

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

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

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

alexandrehsantos
Posts: 1
Joined: Mon May 18, 2015 4:58 pm
Contact:

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

Post by alexandrehsantos »

In this case:

Code: Select all

Object o = a; Runnable r = o;
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?

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

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

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

Roibeard
Posts: 19
Joined: Sun Jul 12, 2015 6:40 am
Contact:

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

Post 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.

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

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

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

Mushfiq Mammadov
Posts: 32
Joined: Fri Oct 16, 2015 4:33 am
Contact:

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

Post by Mushfiq Mammadov »

Maybe Observable should be Observer.
Image

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

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

Post by admin »

Fixed.
thank you for your feedback!
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 45 guests