Reputation: 785
Thread class is said to have a "IS-a" relation as well as "Has-a" relation with the runnable interface. What is the benefit of having both of these?
Why do we give priority to "Has-a" relation?
Upvotes: 1
Views: 64
Reputation: 1503869
What is the benefit of having both of these?
It was a poor design choice, IMO. It would have been cleaner to avoid Thread
implementing Runnable
in the first place. This has led to various bugs (as witnessed by questions on Stack Overflow) where people have called run
on a Thread
instead of start
.
Why do we give priority to "Has-a" relation?
Composition is generally more flexible than inheritance. If you create a Runnable
implementation you can pass that to an ExecutorService
instead of a Thread
, for example. You aren't tying yourself to one particular way of executing the action - you're just saying "this is the code I want to run."
Upvotes: 5