• Post Reply Bookmark Topic Watch Topic
  • New Topic

new keyword with Runnable  RSS feed

 
Gerald Amalraj
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
return new Runnable() {
public void run() {
code...
}

I have seen a code like this. My question is: as Runnable is an interface how we can use it with the new keyword.
As new keyword can be used to creae an object how we can create an object for an interface Runnable.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is an anonymous class. You are creating a class maybe called Foo$1 which implements the interface. You must implement the run method in order for it to compile.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's worth to remember that an anonymous class does not have do be based on interface .
You can use any class/interface/
The class can't be final and must have a constructor that is accessible. In other words: you need to be able to extend that class.

An example:
 
Gerald Amalraj
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:It is an anonymous class. You are creating a class maybe called Foo$1 which implements the interface. You must implement the run method in order for it to compile.


so, is it like if we are creating an anonymous class, new keyword can even be used with an interface.

can anyone explain in detail what happens if we are using a new keyword for an interface while creating an anonymous class?

Regards
Gerald.J
 
Gerald Amalraj
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:It is an anonymous class. You are creating a class maybe called Foo$1 which implements the interface. You must implement the run method in order for it to compile.


Hi,

Thanks for your reply.

This is the code block I came across.

private final Runnable createRemoveRunnable(final String name) {
return new Runnable() {
public void run() {
objects.remove(name);
expire.remove(name);
}
};
}

Regds
Gerald.J

 
Gerald Amalraj
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, Anonymous class can even consider an interface as class. It even creates an object for an interface?
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gerald Amalraj wrote:So, Anonymous class can even consider an interface as class. It even creates an object for an interface?

No. It does not allow to treat interface as a class. It creates a new object of a class with no name (thus anonymous). And this class implements an interface.

You can read more about it here.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!