1

Possible Duplicate:
Java: “implements Runnable” vs. “extends Thread”

I have two questions about multithreaded programming. I read a few answers on the internet, but still I can't find a satisfying answer.

  1. Implementing the Runnable is preferred over extending the thread class. Why?

  2. How is it that we are able to get away with overriding just the run() method?

According to 'The Complete Reference to Java' by Herbert Schildt, If we are not overriding any methods of Thread class other than run(), it's better we implement Runnable.

My 2nd question might sound a bit silly, but I seem to be missing something and I'm not sure about how the whole thing works.

Community
  • 1
  • 1
Vishnu
  • 446
  • 1
  • 7
  • 10

4 Answers4

5

1: Implementing the Runnable is preferred over extending the thread class. why?

Because it allows your class to extend another class if it wants to instead of being forced to extend Thread.

2: How is it that we are able to get away with over-ridding just the run() method?

You can certainly override other methods but the Thread object will call the run() method when the thread is started. That's how it works. The default Thread.run() method is:

public void run() {
    if (target != null) {
        target.run();
    }
}

If you call the Thread constructor with a Runnable then that is what the target is set to. If you instead extend Thread and @Override the run() method then that is the method that will be called when the thread object is started.

That's how the Thread class works.

2: How is it that we are able to get away with over-ridding just the run() method?

You may have misspoke here and were instead asking why we only need to implement the run() method in Runnable().

  • Runnable is an interface with a single run() method that you must implement.
  • Thread is a concrete class that you are extending. You can override any methods in a concrete class unless the class is final or the method is final.

Make sure you use proper Java terminology and don't mixup implement and override.

Gray
  • 115,027
  • 24
  • 293
  • 354
  • @Gary-But we can implement a class only when we override all the methods of the class? as in a abstract class. Am i not right? – Vishnu Mar 07 '12 at 16:26
  • @vishnu You can't implement a class. You only extend it. See my answer. – adarshr Mar 07 '12 at 16:26
  • @vishnu: If you are implementing an interface, you need to override all the methods in that interface unless your class is abstract. If you are extending an abstract class, you need to implement all abstract methods unless your class is itself abstract. If you are extending a non-abstract class, there is nothing to implement. – Jeremy Mar 07 '12 at 16:28
  • @vishnu Jeremy and adarshr are spot on. One more thing though. `Thread` is actually a concrete class -- not abstract. You can also extend concrete classes (that aren't final) and override concrete methods (again that aren't final). `Thread.run()` is a concrete method in a concrete class. – Gray Mar 07 '12 at 16:30
  • @adarshr-ah mistake on my part. Pretty stupid i say. To make myself more clear i think i should rather put my question as..when i implement an interface i should override all the methods of the interface. But i'm overriding just run() – Vishnu Mar 07 '12 at 16:32
  • @JeremyHeiler-thanks that clears my doubt to some extent. – Vishnu Mar 07 '12 at 16:34
  • @vishnu `Thread` is a concrete class that you are _extending_. `Runnable` is an interface with a single `run()` method that you need to implement. – Gray Mar 07 '12 at 16:35
  • @Gary-so implementing a concrete class is a bit different from implementing a interface. I hope i got it right – Vishnu Mar 07 '12 at 16:35
  • 1
    Additionally, there are a variety of utilities that accept `Runnable`s and do their own thread management on their own -- `ExecutorService`, for example. Using `Runnable` lets you use those utilities or not as you desire. – Louis Wasserman Mar 07 '12 at 16:36
  • @gary-please ignore my earlier comment. I get it now. Thank you very much. – Vishnu Mar 07 '12 at 16:36
  • @vishnu Make sure you use proper terminology. You _extend_ an `abstract` or a concrete class. You _implement_ an `interface`. These terms are very important in Java. – Gray Mar 07 '12 at 16:39
0

Three fundamentals before you delve deeper.

  • A class can only extend one class.
  • A class can implement any number of interfaces.
  • An interface can extend any number of interfaces.

Additionally, an abstract class is a class. So it behaves like a class and you can't implement it. You only extend it.

adarshr
  • 61,315
  • 23
  • 138
  • 167
0

There are a number of traps which can occur if you extend Thread which you don't get with implementing Runnable.

public static String getName() {
    return "My Test Application";
}

public static void stop() {
    System.out.println("Shutting down");
}

public static void main(String... args) {
    new Thread() {
        @Override
        public void run() {
            System.out.println(getName());
            stop();
        }
    }.run();
    System.out.println("Bye now.");
}

prints

Thread-0
Bye now.

See how many bugs you can spot.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

Unnecessary inheritance is generally a bad idea. You might hear "prefer composition over inheritance" often enough. I'm sure google can pull up enough links for you. The general issue is that inheritance bring tight, unnecessary coupling between classes.

Same goes for GUI components, btw.

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305