Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
5 views
in Java by (4k points)

What is the difference between Implements Runnable and extends Thread in Java, For reference see these codes:

implements Runnable:

public class MyRunnable implements Runnable {

public void run() {

//Code

}

}

//Started with a "new Thread(new MyRunnable()).start()" call

extends Thread:

public class MyThread extends Thread {

public MyThread() {

super("xyz");

}

public void run() {

//Code

}

}

//Started with a "new xyz().start()" call

Can anyone answer this?

1 Answer

0 votes
by (46k points)

The difference between them is:

1. Reusability: implement Runnable can be reused whenever the user wants to whereas extend Threads is specific in behavior and hence can't be reused.

2.  Functions overhead:

We can easily perform a task using implement Runnable without the Threads class functions overhead whereas in extends Threads we need to inherit all the functions of the Thread class which we might not need.

3. Object-Oriented Design:

extends Thread is not a good Object Oriented practice whereas implementing Runnable is preferred as it doesn't specialize or modify the thread behavior.

4.  Inheritance Option:

Implementing Runnable gives choice to extend a class we like whereas extends Thread can't support multiple inheritances.

5.  Loosely-coupled:

implements Runnable makes the code loosely-coupled and easier to read whereas extend Thread makes the code tightly coupled.

Related questions

0 votes
1 answer
asked Jul 10, 2019 in Java by alan99 (830 points)
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...