Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please help regarding creating a thread object without actually extending thread class

 
Kay Kingston
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone please explain how the Thread object is created without actually extending thread class in the following code


 
harshvardhan ojha
Ranch Hand
Posts: 157
1
Android Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kay,

You have two ways to create a Thread in Java
1. Extend Thread Class
2. Implement Runnable interface

in both scenarios you need to override public void run() method.

You have chosen here 2nd way.

public class RunnableTest extends Addition implements Runnable

also you got public void run() from superclass. Got it??
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kay Kingston,

Welcome to CodeRanch!

I hope Harshvardhan's answer will be helpful to you.

Also, please make a practice to UseCodeTags in your code. It makes reading of code much easier.
 
Kay Kingston
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
harshvardhan ojha wrote:Hi Kay,

You have two ways to create a Thread in Java
1. Extend Thread Class
2. Implement Runnable interface

in both scenarios you need to override public void run() method.

You have chosen here 2nd way.

public class RunnableTest extends Addition implements Runnable

also you got public void run() from superclass. Got it??


Hey thanks for the quick reply
Do you mean run() method is inherited from Addition into RunnableTest by your last statement?
 
Kay Kingston
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anayonkar Shivalkar wrote:Hi Kay Kingston,

Welcome to CodeRanch!

I hope Harshvardhan's answer will be helpful to you.

Also, please make a practice to UseCodeTags in your code. It makes reading of code much easier.


Hi thanks
I will
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kay Kingston wrote:
harshvardhan ojha wrote:
also you got public void run() from superclass. Got it??


Hey thanks for the quick reply
Do you mean run() method is inherited from Addition into RunnableTest by your last statement?

Yes, that is correct. And doing so fulfills the Runnable interface's requirements. But it isn't really good practice since it makes it harder to associate the implemented method (run()) with the interface that requires it (Runnable). Either Addition should implement Runnable since it has the run() method, or the run() method should be moved to RunnableTest class since it implements Runnable. Either way, the 'implements Runnable' and run() method should be in the same class to keep things easy.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic