Difference between extends thread class vs implements runnable interface

Difference between extends thread class vs implements runnable interface

View Answers

October 1, 2009 at 5:35 PM

Hi Friend,

Difference:

1)If you want to extend the Thread class then it will make your class unable to extend other classes as java is having single inheritance feature whereas If you implement runnable interface, you can gain better object-oriented design and consistency and also avoid the single inheritance problems.

2)Extending the thread will give you simple code structure in comparison to Runnable Interface.

3)Using Runnable Interface, you can run the class several times whereas Thread have the start() method that can be called only once.

Thanks

July 24, 2012 at 10:49 AM

The main difference between extending a Thread class and implementing the Runnable interface is, Extending the Thread class:When your class is extending the Thread class then your class becomes a single thread which is inheriting all the properties and characteristics of the Thread class and it becomes heavy. Implementing the Runnable interface:When your class implements the Runnable interface then you only override the run() method,but you create one or more instance(s) of the Thread class in that instance you pass the instance of your Runnable interface implemented class and then you apply the start() method on the Thread instance.So this instance(s) creates a seperate Thread(s) and every individual Thread runs seperately but not as a single heavy Thread in your program.









Related Tutorials/Questions & Answers:
Difference between extends thread class vs implements runnable interface - Java Interview Questions
Difference between extends thread class vs implements runnable interface  Hi Friends, can you give difference between extending thread class... Runnable Interface, you can run the class several times whereas Thread have
implements runnable n extends thread - Java Beginners
implements runnable n extends thread  what is the difference between implements runnable n extends thread? public class...(); class StringThreadImplement implements Runnable { private String } }  Hi public class
Advertisements
what is the difference between extends and implements
what is the difference between extends and implements  difference between extends and implements
what is the difference between extends and implements
what is the difference between extends and implements  difference between extends and implements
Diff between Runnable Interface and Thread class while using threads
Diff between Runnable Interface and Thread class while using threads  Diff between Runnable Interface and Thread class while using threads   Hi Friend, Difference: 1)If you want to extend the Thread class
Diff between Runnable Interface and Thread class while using threads
Diff between Runnable Interface and Thread class while using threads  Diff between Runnable Interface and Thread class while using threads   Hi Friend, Difference: 1)If you want to extend the Thread class
Difference between abstract class and an interface
Difference between abstract class and an interface The difference between... between abstract class and interface in java. Abstract class is a class... search for overridden methods. There is no any difference between a interface
What are the difference between abstract class and interface?
What are the difference between abstract class and interface?  What are the difference between abstract class and interface
Difference between Timer and Thread?
Difference between Timer and Thread?  Can anyone tell me about the difference between Timer and Thread, Why we need to have Timer in case we have Thread implimentation startegy in Java
Interface vs abstract class
Interface vs abstract class  Hai, Is there any possible to defining abstract methods in Interface
difference between enum and final class?
difference between enum and final class?  what is the difference between enum and final class in java?   The java.lang.Enum is an abstract.... The definition of Enum is: public abstract class Enum> extends Object implements
Runnable interface in java
In this section we will learn about Runnable interface in java. In java thread are created by two ways, one by extending Thread class and another one by implementing Runnable interface
Java Thread and Runnable
Java Thread and Runnable  What's the difference between Thread and Runnable types
Interface Vs Abstract Class
Interface Vs Abstract Class       There are three main differences between an interface and an abstract..., an interface is equivalent to a fully abstract class (a class with only public abstract
difference between main thread and child thread?
difference between main thread and child thread?  any one give correct exact difference.   in jsp 7 implicit objects are available those are 1:response->This denotes the data included with the HTTP Response. 2
what is the difference between moqui framework vs other frameworks
what is the difference between moqui framework vs other frameworks  HI Can you share what is the difference between Moqui framework vs other frameworks
about implements and extends - Java Beginners
about implements and extends  hello, class A extends B implements c // this is valid statement class A implements c extends B // this is invalid... A extends B implements C but class A implements C extends B is invalid plz explain
Java Runnable Interface
is an interface. Thread class implements it. Java has multithreading facility. Thread is also created by implementing the Runnable interface. Java Runnable Thread Example public class runnable1 implements Runnable { @Override
Difference between GenericServlet and HttpServlet
In this section we will discuss about the difference between GenericServlet and HttpServlet. GenericServlet is an abstract class that extends java.lang.Object while HttpServlet is an abstract class that extends GenericServlet
can we use class in jsp which implements interface
can we use class in jsp which implements interface   can we use class in jsp which implements interface
Java : Runnable Thread
methods too. After creating class that implements Runnable interface, you can... a thread by implementing the Runnable interface. You need to implement a single... are creating thread by using Runnable interface.ADS_TO_REPLACE_2 class
Interface and Abstract class
Interface and Abstract class  Difference between Interface and Abstract class? Give some example   Difference Between Interface and Abstract Class 1)Main difference is methods of a Java interface are implicitly
Difference between the local and class variable.
class. Below two program are shown which help you to understand the difference between the class and local variable. Showing Local Variable...Description: The class variable are declared in the class but not within
Difference between Java IO Class - Java Beginners
Difference between Java IO Class  What is the difference in function between Two set of Stream class as mention below- 1)FileInputStream & FileOutPutStream vs. 2)InputStreamReader & OutputStreamWriter
difference between marker and tag interface - Java Interview Questions
. A class implements this interface to indicate that its non-transient data members can...difference between marker and tag interface  what is the difference between marker interface and tag interface?  Hi friend, nterface
What’s the difference between accessing a class method via -> and via ::?
What?s the difference between accessing a class method via -> and via ::?  What?s the difference between accessing a class method via -> and via
What is the difference between a static and a non-static inner class?
What is the difference between a static and a non-static inner class?   Hi, What is the difference between a static and a non-static inner class? Thanks
Difference between class,name,id attributes in each JSP tag
Difference between class,name,id attributes in each JSP tag  Can i know the difference between class,name,id attributes in each JSP tags.................. These 3 are only used for reference ..... And what are the differernces
Threads on runnable interface - Java Beginners
.  Hi class MyThread extends Thread{ MyThread(String s...Threads on runnable interface  need a program.....please reply asap Create 2 threads using runnable interface.First threads shd print "hello
Threads on runnable interface - Java Beginners
.  Hi manshi, i am sending simple code of thread. class MyThread extends Thread{ MyThread(String s){ super(s); start...Threads on runnable interface  need a program.....please reply asap
Java Thread class
It is created by extending the Thread class or implementing Runnable interface Java Thread Class Example public class thread1 extends Thread { @Override... Java Thread Class is a piece of the program execution Java has
Java Interview Questions - Page 5
;extends" Thread class. Second, making your class "implements" Runnable interface. Put jobs in a run() method and call start() method... of the Thread class, the JVM invokes the thread's run() method when the thread
Difference between DispatchAction and LookupDispatchAction
Difference between DispatchAction and LookupDispatchAction  What is the Difference between DispatchAction and LookupDispatchAction
difference between SessionState and ViewState
difference between SessionState and ViewState  What is the difference between SessionState and ViewState
difference between ForwardAction and IncludeAction
difference between ForwardAction and IncludeAction  What is the difference between ForwardAction and IncludeAction
Difference between struts and JSF
Difference between struts and JSF  What is the difference between struts and JSF
difference between == and === operators?
difference between == and === operators?  Is (====) operator available in java or not? difference between
difference between varchar & varchar2?
difference between varchar & varchar2?  What is the difference between varchar & varchar2
Difference between 3d and 4d
Difference between 3d and 4d  what is the difference between 3d and 4d
What is the difference between the >> and >>> operators?
What is the difference between the >> and >>> operators?   hi, What is the difference between the >> and >>> operators? Thanks
Creation of Multiple Threads
interface: class MyThread1 implements Runnable{   Thread t;   MyThread1.... It means, you can use either class Thread or interface Runnable to implement thread... than one thread (multithreads) in a program using class Thread or implementing
Creation of MultiThreads
the Runnable interface: class MyThread1 implements Runnable{   Thread t.... It means, you can use either class Thread or interface Runnable to implement... create more than one thread (multithreads) in a program using class Thread
abstract class and interface - Java Beginners
. For difference between interface and abstract class visit to : http...? when should we use an abstract class? when should we use interface instead... look at a Dog and Cow subclass that extends the Animal class. public Dog
Thread
Thread  class Extender extends Thread { Extender(Runnable run... :"); //new Thread(new Implementer()).start(); } } class Implementer implements Runnable { public void run(){ System.out.println("ohhh
Abstract class and interface in Java
Abstract class and interface in Java  What is the difference between abstract class and interfaces in Java?   Differences between an interface and an abstract class: At the same time multiple interfaces can
Difference between Mysql and SQL
Difference between Mysql and SQL  hello, What is the difference between Mysql and SQL??   hii,ADS_TO_REPLACE_1 SQL is structural quary language but mysql is database package
Interface and Abstract class
Interface and Abstract class  hello,, Can some body tell me what is the difference between an Interface and an Abstract class?   hi,ADS_TO_REPLACE_1 Very Good Question Abstract class is a class which contain one
Java Exception Thread
class RunnableThread implements Runnable { Thread runner; public... class RunnableThread implements Runnable { Thread runner; public...)Extends the Threads Class( java.lang.thread)ADS_TO_REPLACE_3 2)Implement Runnable
Difference between SCJP Exams
Difference between SCJP Exams  What is the differences between SCJP 5 (310 - 055) exam and SCJP 6 (310 - 065) exam??? Thank You In Adv
Difference between JSP and Servlets
Difference between JSP and Servlets  What is the difference between JSP and Servlets ?   JSP is used mainly for presentation only. A JSP can only be HttpServlet that means the only supported protocol in JSP is HTTP

Ads