Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Thread not an abstract class and start() not final?

Why was the Thread class implemented as a regular class and not an abstract class with run() method being abstract.

Will it possibly introduce any problems? Or does it have any use in being this way?

Also, the Thread.start() method is supposed to be a very specific method whose functionality cannot be implemented by any other class (If I am not wrong). And hence I guess the final keyword would be apt for this more than any other method.

But I am able to override this method and use it as I like,

public class Test extends Thread {     public static void main (String... args) {         Thread test = new Test();         test.start();     }      @Override     public void run() {         System.out.println("New thread started...");     }      @Override     public void start() {         System.out.println("Did anyone tell you I will spawn a new thread??");     } } 

It obviously only printed,

Did anyone tell you I will spawn a new thread??

Is there any use in overriding other than confusing the engineer replacing you?

If not, why was the method not declared final in Thread class?

like image 380
Codebender Avatar asked Jul 03 '15 09:07

Codebender


People also ask

Why a class Cannot be both abstract and final?

If you declare a class abstract, to use it, you must extend it and if you declare a class final you cannot extend it, since both contradict with each other you cannot declare a class both abstract and final if you do so a compile time error will be generated.

What is the difference between start () and run () method of thread class?

start method of thread class is implemented as when it is called a new Thread is created and code inside run() method is executed in that new Thread. While if run method is executed directly than no new Thread is created and code inside run() will execute on current Thread and no multi-threading will take place.

Why an abstract method Cannot be defined as final?

because as soon as you declare an abstract method in a Java class, the class automatically becomes an abstract class and you cannot make an abstract class final in Java as discussed before, hence it's not possible to have an abstract method in a final class in Java.


2 Answers

You can of course choose to shoot yourself in the foot, but that doesn't mean you must.

Why was the Thread class implemented as a regular class and not an abstract class with run() method being abstract.

Because the recommended way to create a start a thread is not to subclass Thread. The recommended way is to define a Runnable, and pass it as argument to the Thread constructor:

Runnable r = new Runnable() {     @Override     public void run() {         ...     } }; Thread t = new Thread(r); t.start(); 

And hence I guess the final keyword would be apt for this more than any other method.

Yes and no. You can't replace the implementation of start() by your own implementation, but you can do additional things in start() if you want:

@Override public void start() {     System.out.println("Did anyone tell you I will spawn a new thread??");     super.start(); } 

That said, if Java was redesigned from scratch today, there is a good chance the design would be different. Remember that this class dates from Java 1.0, and is still backward-compatible.

like image 174
JB Nizet Avatar answered Oct 18 '22 11:10

JB Nizet


Why was the Thread class implemented as a regular class and not an abstract class with run() method being abstract.

This question actually boils down to the fact that you should always prefer composition over inheritance.

If the Thread class was declared as abstract, the language would have to provide another class that extended from it which programmers could use to create a Thread. Your question would then be about why this class that extends from Thread is not abstract. If the language did not provide another class that extends from Thread, programmers would have to create their own class that extends from Thread and override the run() method.

If not, why was the method not declared final in Thread class??

The only possible explanation I can give is that the developers of the language saw some use-cases for overriding start when the class was introduced to the JDK. The first version of Java that I used was 1.5 and I personally have not come across a use-case where I found the need to override start. As JB Nizet stated in his answer

if Java was redesigned from scratch today, there is a good chance the design would be different

like image 42
Chetan Kinger Avatar answered Oct 18 '22 10:10

Chetan Kinger