2

I always wonder how just by implementing an interface , sub class acquires the behavior. For example if I implement Runnable interface my subclass start behaving as thread, but what if I implement all the methods defined in interface Runnable but not write "implementing Runnable", subclass doesn't behave as Thread. Same with EventListeners . Please help me understanding this behavior.

user1147070
  • 491
  • 7
  • 21
  • 1
    "For example if I implement Runnable interface my subclass start behaving as thread" is wrong; interfaces are just contracts. – Scorpion Jun 03 '13 at 09:52
  • Please check this out. http://stackoverflow.com/questions/12966044/java-threading-how-does-implementing-runnable-work-for-threading – Santosh Jun 03 '13 at 09:53

7 Answers7

2

By implementing an interface I, you're declaring that the Object "is a" I and that it'll contain all the methods defined by this interface. If you just implement the methods of the interface I, but don't declare it by an implement statement, compiler won't be able to determine that your class "is a" I and you won't be able to use it as a I-type.

eternay
  • 3,754
  • 2
  • 29
  • 27
1

No, Runnable has nothing to do with behaving like a thread. It just contains a plain, simple, void nullary method called "run".

Not specifying implements Runnable will just make your object not an instance of the Runnable type, which means you won't be able to pass it to a method requiring a Runnable. This is just an issue of type safety. The method you call could also accept an Object and invoke run using reflection, with the exact same behavior.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • So is the method "run" of my thread is invoked through reflection in java? Sorry I am new to java.. – user1147070 Jun 03 '13 at 09:55
  • @user1147070 - The `java.lang.Thread` class is defined as part of the standard library, and one way of constructing a `Thread` is to pass in an instance of `Runnable`. The Thread code knows that it can call `Runnable.run()`, because that method is defined on the interface - no reflection needed. Marking your class as `implements Runnable` just lets the compiler, and other classes, know that they can "treat it like a Runnable". It doesn't as such inherit any behaviour. – Andrzej Doyle Jun 03 '13 at 09:58
  • Thanks. I get the idea about Runnable. But many a times just by implementing an interface subclass start behaving like that. Is that through reflection and how? – user1147070 Jun 03 '13 at 10:08
  • No, your idea is wrong. `implements interface` doesn't add absolutely any behavior to a class: it just forces that class to define all the methods of the interface. – Marko Topolnik Jun 03 '13 at 10:19
1

When you are implementing Runnable your class does not become thread and does not start behavior as thread. However if your class implements Runnable you can run it in context of thread:

class MyClass1 implements Runnable {
    public void run() {
        // this stuff will run in thread when thread's start() method is called
    }
}

new Thread(new MyClass1()).start();

But java is strongly typed language. You can just create class like this:

class MyClass2 {
    public void run() {
        // this stuff will run in thread when thread's start() method is called
    }
}

But it will not be Runnable. Therefore you cannot just send it to thread:

new Thread(new MyClass2()).start();

In this case you will get compilation error. Compiler cannot know that your class indeed implements method that looks like one that is declared in Runnable. You must declare this (as in first case).

AlexR
  • 114,158
  • 16
  • 130
  • 208
1

A Runnable only allows your class to be run in a Thread. You still need e.g. a java.util.concurrent.Executor to actually run it in an actual Thread.

However, you can extend Thread which would allow you to call Thread.start().

To actually get out behavior from just implementing an interface, you would need a second object inspecting the classpath for classes implementing your interface using reflection, and then do something with that class.

Marcel Steinbach
  • 1,109
  • 6
  • 9
  • Thanks. I get the idea first idea that why we implement Runnable. Can you please elaborate a little about how can we can get the behavior from just implementing an interface through reflection. – user1147070 Jun 03 '13 at 10:04
  • You could scan all the classes (see http://stackoverflow.com/questions/520328/can-you-find-all-classes-in-a-package-using-reflection for that) for a specific interface, instantiate them and, e.g call a method. But as a note, it is very obscure to do it like that, better explicitly instantiate your classes and call the interface method. – Marcel Steinbach Jun 03 '13 at 12:08
0

Your question has two aspect:

  1. Interface as a contract: Interfaces imposes a behavioral contract on a class implementing it. For example, If Car is an interface with some methods abstractly defining a car, any class implementing Car interface will have to define the methods of the car. You are free to actually implement the behavior.
  2. Analogy of Runnable acting as thread is incorrect. What makes Runnable class act as thread is the the Thread class. Runnable just specifies the contract for a class to act as thread. Check this post.
Community
  • 1
  • 1
Santosh
  • 17,667
  • 4
  • 54
  • 79
0

This is not a subclassing behavior. In order to use subclass behavior you need to extend the class.

W.r.t interfaces these are just templates/contracts that are implemented by class. In order for runnable to work calling program need to instantiate a thread and when thread will start it will call run method implemented by your class.

Harish Kumar
  • 528
  • 2
  • 15
0

I always wonder how just by implementing an interface , sub class acquires the behavior.

It doesn't 'acquire' any 'behaviour'. The subclass provides the behaviour. What implementing the interface does is provide the compile-time type signatures such that you can use the subclass where the interface is specified.

user207421
  • 305,947
  • 44
  • 307
  • 483