• Post Reply Bookmark Topic Watch Topic
  • New Topic

casting from examlab  RSS feed

 
Shardul Kumar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


can anyone xplain this casting problem from line 15 to 20.....
thanks.
 
Vivek Singh
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
works fine for me!!!

Use CODE TAGS!!!
 
Vivek Singh
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vivek Singh wrote:works fine for me!!! No compilation problem

Use CODE TAGS!!!
 
Ankit Garg
Sheriff
Posts: 9610
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Shardul Kumar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 9610
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!