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
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.
"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 ");
}
}
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 option | extends any java class | No |
Reusability | Yes | No |
Object Oriented Design | Good,allows composition | Bad |
Loosely Coupled | Yes | No |
Function Overhead | No | Yes |
If you have any doubts regarding the difference between "implements Runnable" and "extends Thread" then please mention in the comments .