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

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

Moderator: admin

Post Reply
SeoaneR
Posts: 8
Joined: Tue Nov 07, 2017 8:23 am
Contact:

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

Post by SeoaneR »

The questions asks : What can be inserted in the code below so that it will print UP UP UP?

In relation to ((Tone)s).up();
I get a ClassCastException so their is no print output of UP UP UP.
So why is the answer correct?

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

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

Post by admin »

Do you have "implements Tone" in GoodSpeak? Please make sure that you have typed the code exactly as given in the question.
If you like our products and services, please help us by posting your review here.

chiran12345
Posts: 5
Joined: Fri Apr 12, 2019 12:00 pm
Contact:

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

Post by chiran12345 »

I'm confused, why it is possible to cast s to Tone, ((Tone)s).up();

Per my understanding, we can use parent reference to hold child object and can use interface to hold implemented class object. I used 3 fundamental points to remember in Object Type casting: lets say

A b =(C) d; where A and C= class/interface, and b= name of reference variable, d = reference variable name

1 (compile time checking): the type of d and C must have relation either child to parent vice versa or same type. Otherwise we will get compile time error (CE) saying inconvertible types.

2 (compile time checking): C must be either same or derived type of A, otherwise CE saying incompatible types

3(Runitme Checking): Runtime Object type d must be either same or derived type of C, otherwise RE saying ClassCastException.


So, in our case, s and Tone doesn't have relation, why it is not compile time error? Please enlighten me??

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

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

Post by admin »

Point 1 in your list only talks about classes not interface.
In case of an interface, the compiler checks whether it is possible for a reference to point to an object that implements the interface to which it is being casts. So, when you do ((Tone)s).up();, the compiler checks whether s can point to an object of a class that implements Tone. Here, it is possible because type of s is Speak and it is not a final class. So, s can point to a subclass of Speak that implements Tone.
If you like our products and services, please help us by posting your review here.

chiran12345
Posts: 5
Joined: Fri Apr 12, 2019 12:00 pm
Contact:

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

Post by chiran12345 »

Thank you so much

Post Reply

Who is online

Users browsing this forum: No registered users and 94 guests