• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Solve this thread question!! confused!!

 
himanshu kesarwani
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this questuin is from examulator....
i am not geting why its giving compile error telling run() must be overidden....
is is compulsory to override run() when implementing runnable.
is the same case is with extending Thread also.

As what i think is that as no thread object is created there should not be any problem and start() should be run like normal method!!








 
Harshit Rastogi
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please go through the thread basics again,

If you implement Runnable or extend Thread class, run() is the method you need to override and not start(). Start() is used to call start the thread, which in turn calls run() method
 
Cristian Senchiu
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
himanshu kesarwani wrote:...
i am not geting why its giving compile error telling run() must be overidden....
is is compulsory to override run() when implementing runnable.
is the same case is with extending Thread also.
As what i think is that as no thread object is created there should not be any problem and start() should be run like normal method!!

Himanshu,
your class implements the Runnable interface.
Runnable interface declares a method run(), so you have to give implementation for it in your class, whether you use it or not.
And no, it is not the same as extending Thread class , where you can choose if you override or not the run() method.
 
Chaitanya Jadhav
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
* If you implements Runnable you MUST override :- public void run(){}

* If you extends Thread class you not need to override run() method.

see example:

class Test implements Runnable
{
public void run(){
System.out.println("In run Method ");
}
public void start(){
System.out.println("In start Method ");
}
public static void main(String[] args)
{
Test obj=new Test();
obj.start(); // start method call like other normal method
Thread t=new Thread(obj); //
t.start();// Thread.start() method automatically call run() method


}
}
 
himanshu kesarwani
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya this is what i wanted i was olly confused about the extending thread as in k&b it was there......telling about it!!
so i thought that the case must be same for Runnable even.......
thanks guys...
i am solving examulator exam,and getting average 80%....
can you guys tell me more better and FREE mock exams......!!
i heard whizlabs and devaks mock exams are very good but i didnt get them in free........how can i get them in free.... !!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic