• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Help :: Illegal Casting Question

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All , I faced the attached question in Exam Lab, and I still cant figure it out why there are two illegal casts , the reference variable is of the same type we are casting the object to , so it should compile ... please explain
IllegalCasting.JPG
[Thumbnail for IllegalCasting.JPG]
 
Ranch Hand
Posts: 45
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A[] aa1 = (A[]) r1 //Illegal

I think above is Illegal because r1 is just an Interface reference type,not of type array.

ra1 = (Runnable[]) ca; //Illegal

In this case also same situation holds good. If you want to make this work change ca to array type and make C a non final class. OR typecast ca to just Runnable reference by making C a non-final class.
 
hatem odeh
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
will you please tell me the rule so i can memorize for the exam to solve such questions
 
Sheriff
Posts: 9697
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hatem, you just need to understand that you cannot type cast between a single element type and an array type, like



This is applicable for both primitive and objects. The only exception is Object type. Since it is the super type of all the types, you can assign an array to it. So any cast between a reference type will not work.

Now a cast between two non-related classes is not allowed. So if you try



Here String and Integer classes are in different hierarchy so the cast is not allowed. But a cast is allowed between an interface type and another interface type or class.



The basic idea behind this is that at runtime, the cast might be successful as there might be a type, which is sub-type of both the interfaces or one interface or class. The cast between C1 and C2 is not allowed because under no circumstances, can there be an object which is compatible with both (as a class cannot extend multiple classes). Suppose I create a class like this



Now if we review the original type casts, then they will all work



But the cast is not allowed between a non-related class and interface, if the class is final



In this case there cannot be a class, which is a sub-class of both I1 and C1 (as C1 is final). This is why the cast will generate a compilation error.

HTH...
 
Ankit Garg
Sheriff
Posts: 9697
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Balraj Momi wrote:ra1 = (Runnable[]) ca; //Illegal

In this case also same situation holds good. If you want to make this work change ca to array type and make C a non final class.



ca is already of an array type. It is just the matter of C being final...
 
hatem odeh
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ankit a very very very big thanks I am totally in a good shape for any casting questions cause of your explanation
 
Ankit Garg
Sheriff
Posts: 9697
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

hatem odeh wrote:Ankit a very very very big thanks.........



A very very big welcome
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actulally it depends on the device it self, I think you have to change it.

Best regards
 
That's my roommate. He's kinda weird, but he always pays his half of the rent. And he gave me this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic