| Author |
casting from examlab
|
Shardul Kumar
Greenhorn
Joined: May 24, 2009
Posts: 12
|
|
can anyone xplain this casting problem from line 15 to 20.....
thanks.
|
 |
Vivek Singh
Ranch Hand
Joined: Oct 27, 2009
Posts: 92
|
|
works fine for me!!!
Use CODE TAGS!!!
|
 |
Vivek Singh
Ranch Hand
Joined: Oct 27, 2009
Posts: 92
|
|
Vivek Singh wrote:works fine for me!!! No compilation problem
Use CODE TAGS!!!
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
please Use Code Tags when you post a source code. You can edit your message using button and then add code tags to it.
Line 15 is just a normal downcast. m is downcasted to C and automatically upcasted to B (Since class B implements Min and C extends B, thus C IS-A Min). Line 17 is tricky. But suppose the reference m points to an object of type B, then the object pointed by m will be assignable to a reference of type A (as B IS-A A). That's why the cast is allowed. So the fact is, a cast between an interface and a non-final class is always allowed. That's because there might be a class that implements that interface and extends that class. Again line 18 is just a normal downcast. Line 19 downcasts b2 to type C and then it is automatically upcasted to Min. Line 20 is also a normal downcast...
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
Shardul Kumar
Greenhorn
Joined: May 24, 2009
Posts: 12
|
|
Ankit Garg wrote:please Use Code Tags when you post a source code. You can edit your message using  button and then add code tags to it.
Line 15 is just a normal downcast. m is downcasted to C and automatically upcasted to B (Since class B implements Min and C extends B, thus C IS-A Min). Line 17 is tricky. But suppose the reference m points to an object of type B, then the object pointed by m will be assignable to a reference of type A (as B IS-A A). That's why the cast is allowed. So the fact is, a cast between an interface and a non-final class is always allowed. That's because there might be a class that implements that interface and extends that class. Again line 18 is just a normal downcast. Line 19 downcasts b2 to type C and then it is automatically upcasted to Min. Line 20 is also a normal downcast...
but m is null
|
 |
Shardul Kumar
Greenhorn
Joined: May 24, 2009
Posts: 12
|
|
|
please let me clear it that when we do casting is it reference variable casting i mean does object type created play any role???
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
|
Shardul I know that m is null, but that doesn't matter at compile time. The compiler only sees the type of the reference and not the actual object it is pointing to. And null can be type casted to any type so there won't be any exception at runtime...
|
 |
 |
|
|
subject: casting from examlab
|
|
|