5 Difference between "implements Runnable" and "extends Thread" in Java

How to instantiate a Thread in java  either by implementing Runnable or extending Thread class . We will go through the difference between "implements Runnable" and "extends Thread" . It will help us to choose the  correct way to instantiate the Thread in our application.
Although, in the real world application you are much more likely to implement Runnable interface than extends Thread. Extending the Thread class is easiest but not a good Object Oriented practice.
In this post we will see the difference between "implements Runnable" and "extends Thread". This is one of the basic interview question on the topic of Threads.

Read Also :  Life Cycle of Thread in Java 


Difference between "implements Runnable" and "extends Thread" in Java

1. Inheritance Option:   The limitation with "extends Thread" approach is that if you extend Thread,  you can not extend anything else . Java does not support multiple inheritance.  In reality , you do not need Thread class behavior , because in order to use a thread you need to instantiate one anyway.
On the other hand,
Implementing the Runnable interface gives you the choice to extend any class you like , but still define behavior that will be run by separate thread.

2. Reusability :  In "implements Runnable" , we are creating a different Runnable class for a specific behavior  job (if the work you want to be done is job). It gives us the freedom to reuse the specific
behavior job whenever required.
"extends Thread"  contains both thread and job specific behavior code. Hence once thread completes execution , it can not be restart again.  

3Object Oriented Design:  Implementing Runnable should be preferred . It does not specializing or modifying the thread behavior . You are giving thread something to run. We conclude that Composition is the better way. Composition means two objects A and B satisfies has-a  relationship.
"extends Thread"  is not a good Object Oriented practice.

4. Loosely-coupled : "implements Runnable" makes the code loosely-coupled and easier to read .
Because the code is split into two classes . Thread class for the thread specific code and your Runnable implementation class for your job that should be run by a thread code.
"extends Thread"  makes the code tightly coupled . Single class contains the thread code as well as the job that needs to be done by the thread.

5. Functions overhead :  "extends Thread"  means inheriting all the functions of the Thread class which we may do not need .  job can be done easily by Runnable without the Thread class functions overhead.

Example of  "implements Runnable" and "extends Thread" 


public class RunnableExample implements Runnable {
    
    public void run() { 

        System.out.println("Alive is awesome");   

 }
}


public class ThreadExample extends Thread {
    
    public void run() { 

        System.out.println(" Love Yourself ");   

 }
}


difference between  implements Runnable and extends Thread in java


When to use "extends Thread" over "implements Runnable"

The only time it make sense to use "extends Thread"  is when you have a more specialized version of Thread class. In other words , because you have more  specialized thread specific behavior.

But the conditions are that the thread work you want is really just a job to be done by a thread. In that case you need to use "implements Runnable" which also leaves your class free to extend some other class.



Recap : Difference between "implements Runnable"  and "extends Thread"





 implements Runnable extends Thread
Inheritance optionextends any java class No  
ReusabilityYesNo
Object Oriented DesignGood,allows composition Bad  
Loosely CoupledYes No
Function OverheadNoYes





If you have any doubts regarding the difference between "implements Runnable"  and "extends Thread" then please mention in the comments .

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry