• 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

Casting - WHY does Runnable r1 = (Runnable)aa give NO compiler error

 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




Why does not LINE 1 give a compiler error .There is no realtionship between
Runnable and variable aa which is of type A.
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

There is no realtionship between Runnable and variable aa which is of type A.



Which would explain the error
 
Sheriff
Posts: 9707
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

Deepak Bala wrote:

There is no realtionship between Runnable and variable aa which is of type A.


Which would explain the error


Deepak, you forgot, type cast between Interface and Class is always allowed (non-final class to be specific). Simran, the cast is allowed because the cast may be successful at runtime. Here's how

So basically a cast between an unrelated class and interface is allowed because they might have a common subtype (as class B in this case). But a cast between unrelated classes is not allowed because they cannot have a common subtype (as java doesn't allow a class to extend from multiple classes)...
 
Deepak Bala
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deepak, you forgot, type cast between Interface and Class is always allowed (non-final class to be specific).



Yes I missed that. I answered assuming the cast was between 2 unrelated Class types. Thanks
 
Simran Dass
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Thanks a lot Ankit. I always look forward to answers from you.
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


So basically a cast between an unrelated class and interface is allowed because they might have a common subtype (as class B in this case). But a cast between unrelated classes is not allowed because they cannot have a common subtype (as java doesn't allow a class to extend from multiple classes)...



Thanks for the above info Ankit.
 
Greenhorn
Posts: 3
Android Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Will the original "non-implementated" code throw a Runtime Exception?

 
Ankit Garg
Sheriff
Posts: 9707
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

James Bragg wrote:Will the original "non-implementated" code throw a Runtime Exception?


You can try it easily to see what happens, anyways the answer is: Yes...
 
I got this tall by not having enough crisco in my diet as a kid. This ad looks like it had plenty of shortening:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic