Difference Between extends Thread and Implements Runnable

There are two types of approaches in thread creation first one extending Thread class and second one is implements Runnable Interface. If we design threads in first approach then we have to declare one user defined class and which must be extended from “java.lang.Thread” class, in this case, once if we extend Thread.





Class to User-defined class then there is no chance of extending some other classes. If extend some other class to the User Defined thread class then it will represent multiple inheritance, it is not possible in Java.

To resolve the above problem, we have to use second approach to design threads that is implementing “java.lang.Runnable” interface in place of extending “java.lang.Thread” class.


Example:

  • To prepare thread class in first approach,we have to extend Thread class to User defined class.
            class Myclass extends Thread
            {
            }
  • To prepare a Frame in GUI applications,we have to extend java.awt.Frame class to user-defined class.
            class MyClass extends Frame
           {
           }
  • In Java applications,if we want to prepare a thread and a frame by using a single class then we have to extend Thread class and Frame class to a single java class.
            class MyClass extends Thread,Frame
           {
           }
  • The above code is representing Multiple Inheritance, it is not possible in Java. To resolve this problem, we have to use second approach to design thread.
  • In the case of second approach, to design thread we have to implement Runnable interface in user defined class.
             class MyClass extends Frame implements Runnable
            {
            }

Second approach to create Threads and their respective cases:

  1. Declare an user defined class
  2. Implement "java.lang.Runnable" interface in user-defined class.
  3. Implement run() method of "java.lang.Runnable" interface in user-defined thread class.
  4. In main class,in main() method create new thread and access run() method,for this,we have to use the following cases.

           class MyThread implements Runnable
          {
                public void run()
               {

               }
          }

Case 1:

MyThread mt=new MyThread();
mt.start();
Status:Compilation Error
Reason:start() method is not defined in MyThread class and the super class Object class

Case 2:

MyThread mt=new MyThread();
mt.run();
Status:No Compilation Error,but only Main Thread will access MyThread class run() method
like a normal java method, here multi-threading is not happened.

Case 3:

MyThread mt=new MyThread();
Thread t=new Thread();
t.start();
Status: No Compilation Error, here start() method will create a new thread but start() method
will access predefined Thread class run() method with the new thread, it will not access
MyThread class run() method.

Case 4:

MyThread mt=new MyThread();
Thread t=new Thread(mt);
t.start();
Status:No Compilation Error, here start() method will create a new thread and start() method
will access MyThread class run() method with new thread instead of accessing predefined
Thread class run() method.