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

后端 未结 4 1092
北海茫月
北海茫月 2021-01-30 10:05

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

Will it po

4条回答
  •  隐瞒了意图╮
    2021-01-30 10:53

    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

提交回复
热议问题