• Post Reply Bookmark Topic Watch Topic
  • New Topic

Run-time exception from type-casting  RSS feed

 
Mikpo Siru
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

A class 'TestRunnable' implements Runnable.

IThe statements below execute in another class, but they are generating an exception:

Runnable runnable = Thread.getCurrentThread();
TestRunnable tRunnable = (TestRunnable)runnable;

The exception is: "Exception in thread "Thread-3" java.lang.ClassCastException: java.lang.Thread cannot be cast to TestRunnable"

I don't know why the exception is being generated, as both 'tRunnable' and 'runnable' implement Runnable...?

 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would give you an example:

Can a Cat be a Dog? and vice versa? So that's what happens when you try to typecast among the siblings. So its not legal to typecast among the siblings. You can typecast up/down the hierarchy.
 
swaraj gupta
Ranch Hand
Posts: 186
C++ Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not possible to type cast along different class hierarchies.
According to the scenario you have specified-
1> Thread implements Runnable (By Default)
2> tRunnable implements Runnable.
But, there exists no relationship in-between the Thread class and the tRunnable class...
 
Mikpo Siru
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your replies.

The problem is I have this situation:

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's simple. If the object returned is truely an object that is that type, the type cast will succeed.

However, in your example, I am very sure that the Thread object returned is *not* your class type -- as your class type is not a Thread class type. Hence, it is not possible for you to use it to start a thread.

Henry
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mikpo Siru wrote:Thanks for your replies.

The problem is I have this situation:



You could as well call that method from the

of your TestRunnable class. So if its the currently executing thread, it will call the method. Usually your run() method will have the code for that thread of execution. So you can have all the related methods being invoked from the run() method.
 
Mikpo Siru
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

TestRunnable implements Runnable and has a specific method that I want to call. I could make TestRunnable extend Thread. This will solve the type casting problem, but I don't want to restrict my options by extending a class at this stage.

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mikpo Siru wrote:
TestRunnable implements Runnable and has a specific method that I want to call. I could make TestRunnable extend Thread. This will solve the type casting problem, but I don't want to restrict my options by extending a class at this stage.


In other words, you know for a fact that you TestRunnable class is *NOT* a Thread class. And you definitely don't want it to be a Thread class.

So... may I ask why do you think that the Thread object returned by the currentThread() method, could be a TestRunnable object?

Henry
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!