• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Anonymous classes Query

 
Pavan Kumar Dittakavi
Ranch Hand
Posts: 106
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have a small query. I am aware of this concept of Anonymous classes but, how is the following piece of code valid?...Aren't we creating an instance of runnable instance? An we cant instantiate an interface..so how is this allowed..please explain.




Thanks,
Pavan.
 
Rob Spoor
Sheriff
Pie
Posts: 20753
68
Chrome Eclipse IDE Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are not creating an instance of Runnable, you are creating an instance of the anonymous class that implements Runnable.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And just to make the syntactical difference clear:



The curly braces after the parentheses are where you put the body of the concrete class that implements the methods in the interface. You an also create anonymous classes that extend abstract classes and concrete classes this way.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And just to show that you haven't created an instance of the interface, try running this.

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And also:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic