Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Runnable to Thread Casting  RSS feed

 
Balaji Bang
Ranch Hand
Posts: 182
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why the casting gives ClassCastException? Thread class provides the implementation for Runnable interface. Then why cannot we cast???
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is because r1 is an implementer of the Runnable Interface. Thread class also is an implementer of Runnable. But there is no relation between r1(anonymous sub-class referenced by r1) and the Thread class.
 
Balaji Bang
Ranch Hand
Posts: 182
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ohhhhhh. Yes r1 is just an implementor reference not Runnable reference. So we cannot . Thank you
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!