• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmm
terribly mistaken
sorry for that
will look into it deeper
 
Emanuele Ghe
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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



This is wrong, code at LINE 4 compiles succesfully.
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
It's a pleasure to see superheros taking such an interest in science. And this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic