0

How its works without error if we use both extends Thread implements Runnable together ?

public class MyThread extends Thread implements Runnable{

@Override
public void run() {
    // TODO Auto-generated method stub

}
}

Edit: Thread already implements Runnable, so its meaning less to use both.

If I declare a class like this

MyThread extends implements Runnable, Runnable{
   ...
}

Its "Duplicate interface Runnable for the type MyThread", so we cant compile.

Then why we didn't get error for this

public class MyThread extends Thread implements Runnable{ .. }
R D
  • 1,330
  • 13
  • 30
  • 1
    What do you mean by "use them together"? Yes, you can always redeclare which interfaces a class implements, even if the superclass declares that it implements the same interface. (And surely you can see that it's possible just by compiling the code yourself...) – Jon Skeet Feb 28 '17 at 10:46
  • 1
    Thread already implements Runnable. So by extending Thread you automatically implement Runnable. You can still write "extends Thread implements Runnable" but its completly redundand – OH GOD SPIDERS Feb 28 '17 at 10:46
  • 1
    @JonSkeet I think the question was more about *"Is that a good idea to..."* than *"Is that possible..."*. – Mistalis Feb 28 '17 at 10:47
  • 1
    Related: http://stackoverflow.com/questions/20534726/do-subclasses-inherit-interfaces – Erich Kitzmueller Feb 28 '17 at 10:47
  • @Mistalis: Then it should have been asked that way - bear in mind that it originally had different branches for "If yes" and "If no" suggesting that the OP hadn't actually checked first. – Jon Skeet Feb 28 '17 at 11:09
  • You might want to look at how `t.run()` actually is implemented in OpenJDK: http://code.metager.de/source/xref/openjdk/jdk8/jdk/src/share/classes/java/lang/Thread.java – Solomon Slow Feb 28 '17 at 14:51

2 Answers2

1

It won't hurt, but Thread already implements Runnable, so it's repetitive.

lukeg
  • 4,189
  • 3
  • 19
  • 40
0

Yes, We can do it as pointed out above. There is no restriction on that.

But Honestly, I can not think of a logical scenario for it. As interface implies behavior, and If super class is implementing the public behavior(As all methods of interface are public) of interface, child will inherit the implemented behavior from super class.

GKS
  • 299
  • 4
  • 15