Multiple choice

The following block of code creates a Thread using a Runnable target: Runnable target = new MyRunnable(); Thread myThread = new Thread(target); Which of the following classes can be used to create the target, so that the preceding code compiles correctly?

  1. public class MyRunnable extends Runnable{public void run(){}}

  2. public class MyRunnable extends Object{public void run(){}}

  3. public class MyRunnable implements Runnable{public void run(){}}

  4. public class MyRunnable implements Runnable{void run(){}}

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

The class correctly implements the Runnable interface with a legal public void run()method. Option 1 is incorrect because interfaces are not extended; they are implemented. Option 2 is incorrect because even though the class would compile and it has a validpublic void run() method, it does not implement the Runnable interface, so the compiler would complain when creating a Thread with an instance of it. Option 4 is incorrect because the run() method must be public.