• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

casting

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



why one gives error & other exception
i think both have same situation like



 
Sheriff
Posts: 7377
1409
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

gaurav gupta sitm wrote:



The reference type of 'r1' is NOT unknown - it is Runnable.

The casting attempt for t1 does not give you a compile time error as you are casting within a single hierarchy root - you are casting from Runnable to Thread, and Thread is-a Runnable.

The other casting attempt for t2 issues a compile time error as Thread and uvs are not in the same hierarchy root - you are trying to cast from uvs to Thread, but neither uvs is-a Thread nor Thread is-a uvs.
 
gaurav gupta sitm
Ranch Hand
Posts: 47
Redhat Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Devaka Cooray wrote:
The reference type of 'r1' is NOT unknown - it is Runnable.
The casting attempt for t1 does not give you a compile time error as you are casting within a single hierarchy root - you are casting from Runnable to Thread, and Thread is-a Runnable.



sir if i am following your concept then there will be exception ...
but K&B book says that there is Plain-Old Anonymous Inner Classes, Flavor Two.
Anonymous class would be an implementer of the interface Runnable
it's not instantiating a Runnable object, it's creating an instance of a new, anonymous, implementer of Runnable.
Declare a reference variable r1 of type Runnable that, obviously, will refer to an object from a class that implements the Runnable interface.

so there is a Anonymous class that implement Runnable
like : class uvs implements Runnable
so what is diffrence in root hierarchy


sir please clear my concept ....
Thanks

 
Devaka Cooray
Sheriff
Posts: 7377
1409
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Similar to what explained in the book, r1 does NOT instantiate the Runnable interface (it's not possible too). However, there is a reference type for r1 - that is Runnable. The reference type is what checked at the compile time. In case of the second cast, the variable 'u' has 'uvs' as its reference type. You are getting the error due to the fact that uvs and Thread are not in the same hierarchy root.
 
gaurav gupta sitm
Ranch Hand
Posts: 47
Redhat Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks sir my concept is clear now 100% emoticon('');
and also thanks for valuable Exam Lab simulator.
 
if you think brussel sprouts are yummy, you should try any other food. And this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic