Discussion :: Threads
-
Which two can be used to create a new Thread?
- Extend java.lang.Thread and override the run() method.
- Extend java.lang.Runnable and override the start() method.
- Implement java.lang.Thread and implement the run() method.
- Implement java.lang.Runnable and implement the run() method.
- Implement java.lang.Thread and implement the start() method.
Answer : Option C
Explanation :
There are two ways of creating a thread; extend (sub-class) the Thread class and implement the Runnable interface. For both of these ways you must implement (override and not overload) the public void run() method.
(1) is correct - Extending the Thread class and overriding its run method is a valid procedure.
(4) is correct - You must implement interfaces, and runnable is an interface and you must also include the run method.
(2) is wrong - Runnable is an interface which implements not Extends. Gives the error: (No interface expected here)
(3) is wrong - You cannot implement java.lang.Thread (This is a Class). (Implements Thread, gives the error: Interface expected). Implements expects an interface.
(5) is wrong - You cannot implement java.lang.Thread (This is a class). You Extend classes, and Implement interfaces. (Implements Thread, gives the error: Interface expected)
Be The First To Comment