casting
posted 14 years ago
why one gives error & other exception
i think both have same situation like
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
why one gives error & other exception
i think both have same situation like
posted 14 years ago
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.
-
1 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
Author of ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- OCPJP Online Training -- Twitter -- How to Ask a Question
posted 14 years ago
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
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
Author of ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- OCPJP Online Training -- Twitter -- How to Ask a Question
| And inside of my fortune cookie was this tiny ad: Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |












');