-2

I've seen 2 ways of using a Thread class:

// 1
public class Main extends Thread {
    public void run() {
        System.out.println("abc");
    }
    public static void main(String[] args) {
        Thread t = new Thread(new Main()); 
        t.start();

    }
}

// 2
class Main2 extends Thread {
    public void run() {
        System.out.println("abc");
    }
    public static void main(String[] args){
        Main2 m = new Main2();
        m.start();

    }
}

What are the differences between those 2?

Which way is correct?

1 Answers1

1

1 is possible, but superfluous. It works because Main implicitly implements Runnable since it extends Thread. We would use 1 if Main would only implements Runnable, but not extends Thread.

The "correct" approach is 2.

Turing85
  • 18,217
  • 7
  • 33
  • 58