-6

I want to clarify a doubt in multi threading in java. Cant we create an interface with run() and implement it instead of Runnable interface. Will it work?

  • 8
    Well how would you expect to pass it to the `Thread` constructor? This is pretty easy to try - so I suggest you delete this question, try it (the research you should do before asking a question), then ask a question if you still need any clarification. – Jon Skeet Sep 20 '15 at 08:13
  • 1
    If you mean create your own interface with a `run()` method that isn't called the common `Runnable` in Java, then you can, except pretty much all of the concurrency libraries won't recognize it. Why would you want to do this? – Ownaginatious Sep 20 '15 at 08:14

1 Answers1

1

No it won't work.

Or at least, it won't work unless your interface implements the real Runnable interface.

Java inheritance is NOT based on duck typing. Two unrelated interfaces with the same method signatures are not type compatible in the Java type system.

Reference:

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216