• Post Reply Bookmark Topic Watch Topic
  • New Topic

can we instantiate Runnable?  RSS feed

 
Nagarjuna Battula
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thread thread = new Thread(new Runnable() {
public void run() {
}
});

Here if Runnable is an interface then how can you use new Runnable() ???
which in turn instantiating an interface.
Justify Interface cant't be instatiated(Please forget about Dynamic method Dispatch)
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.
In future, please don't use extra colors. I have edited them out this time.

Recommended reading http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not the interface which is being instantiated, a new anonymous class is created which implements Runnable and that is then instantiated.
You can read more about anonymous classes here:http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html
 
Randall Twede
Ranch Hand
Posts: 4696
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is the most common use. This is the way your main() method should look if you are using swing. Word is the name of the class that contains main().
 
Syed Naved Ali
Greenhorn
Posts: 8
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nagarjuna Battula wrote:Thread thread = new Thread(new Runnable() {
public void run() {
}
});

Here if Runnable is an interface then how can you use new Runnable() ???
which in turn instantiating an interface.
Justify Interface cant't be instatiated(Please forget about Dynamic method Dispatch)


Here you are not instantiating any interface(i.e Runnable). What exactly you are doing is creating and instantiating an anonymous class of type Runnable.

Go ahead and make yourself aware of Anonymous class concept as suggested by "Maneesh Godbole".
 
Surapuraju Venkatakishor
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when you go for this kind of code, you have ideal implementation of an interface.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!