aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Need help in questions about casting and assignment from ExamLab final test (answers included) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Need help in questions about casting and assignment from ExamLab final test (answers included) " Watch "Need help in questions about casting and assignment from ExamLab final test (answers included) " New topic
Author

Need help in questions about casting and assignment from ExamLab final test (answers included)

Emanuele Ghe
Ranch Hand

Joined: Feb 04, 2009
Posts: 111
Hi,
I did examlab final test, scoring 63%, but there are 2 questions about assignment and casting that I failed, and I can't understand why things work the way they work ;)
I am really struggling about this, since I will attempt my exam the next week.

Here is the first question with answer:



I can't understand why the last 4 assignments are legal/illegal, as indicated in the figure.
Here are my thoughts, I hope someone can confirm them to me:

A r2 = (A) r1;
is legal because, at runtime, r1 could refer to some subclasses of A (that also implements Runnable and got assigned to the Runnable reference variable r1) ?

A[] aa1 = (A[]) r1;
is illegal because you can never cast a "normal object reference" to an "array object reference" ?

A[] aa2 = (A[]) ra1;
legal for the same reason of { A r2 = (A) r1; } ?

ra1 =(Runnable[]) ca;
is illegal because C is a final class and so is not possible that there will be some subclasses that implements runnable ?


Can someone explain to me ?


Here is the second question:


code at LINE 1, LINE 2, LINE 3 do not compile, and LINE 4 does.

Here are my thoughts:
LINE 1 do not compile because you can never cast a "normal object reference" to an "array object reference" ?

LINE 2 do not compile for the same reason of LINE 1 ?

LINE 3 do not compile because B is final, so it is impossible that there will be some subclass of B implementing I ?

LINE 4 compiles ... Why ?


I understand this is a long question to answer, and I appreciate everyone that will help me.


Thanks in advance.


SCJP6 with score 90%. I am conscious of my ignorance and ready to learn from everyone.
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446
    
    1

hmm
terribly mistaken
sorry for that
will look into it deeper


SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right
Emanuele Ghe
Ranch Hand

Joined: Feb 04, 2009
Posts: 111

now in the line
A[] gred = (B[]) gread ; the compilation fails


This is wrong, code at LINE 4 compiles succesfully.
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446
    
    1

well well
really sorry for answering the question wrong totally
may I ask you something?
why do you think that the lines should not compile?
just asking I am not cross questioning you
because I am here to learn also
if you can put your opinions
then we can discuss more

according to me
the answer is a little simple and I did not think about it before
I think

A and B are in the same inheritance tree and hence we can cast the arrays of superclass to type of subclass at COMPILE TIME but it throws the ClassCastException
as follows
I edited the code a little bit and compiled and run


and this gives the runtime exception
ClassCastException
as we cannot convert an A[] type into B[] type
Emanuele Ghe
Ranch Hand

Joined: Feb 04, 2009
Posts: 111
Prasad Kharkar wrote:well well
really sorry for answering the question wrong totally
may I ask you something?
why do you think that the lines should not compile?
just asking I am not cross questioning you
because I am here to learn also
if you can put your opinions
then we can discuss more

according to me
the answer is a little simple and I did not think about it before
I think

A and B are in the same inheritance tree and hence we can cast the arrays of superclass to type of subclass at COMPILE TIME but it throws the ClassCastException
as follows
I edited the code a little bit and compiled and run


and this gives the runtime exception
ClassCastException
as we cannot convert an A[] type into B[] type



I agree,
it was simple, my mind was just overwhelmed by the studies ;)

Thanks for helping me mate.
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446
    
    1

you are welcome !!
another thing I wanted to tell
if you find anything wrong with my post
you can tell me directly
don't ignore if you did not find the answer
tell me directly if I am wrong
wish you good luck for the exam
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Hello,

Waiting for your result Eman, just dropped a note to your gmail. Will be back to ranch day after as my holidays are starting.

Anxiously waiting for Eman Result,

Prithvi,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help in questions about casting and assignment from ExamLab final test (answers included)