Wednesday 23 September 2015

Thread Class vs Runnable Interface

The most common difference is
  • When you extends Thread class, after that you can’t extend any other class which you required. (As you know, Java does not allow inheriting more than one class).
  • When you implements Runnable, you can save a space for your class to extend any other class in future or now.
However, the significant difference is.
  • When you extends Thread class, each of your thread creates unique object and associate with it.
  • When you implements Runnable, it shares the same object to multiple threads.

object oriented designs have some guidelines for better coding.
  • Coding to an interface rather than to implementation. This makes your software/application easier to extend. In other words, your code will work with all the interface’s subclasses, even ones that have not been created yet.
  • Interface inheritance (implements) is preferable – This makes your code is loosely coupling between classes/objects.(Note : Thread class internally implements the Runnable interface)

Reference:
https://manikandanmv.wordpress.com/tag/extends-thread-vs-implements-runnable/

No comments:

Post a Comment