This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Interfaces in java  RSS feed

 
Sergio Barcelona
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, i'm studing java but i have a problem for understand the interfaces , the first  rule is , you can`t instanciate one interface because is abstract , but reading in the web code I see this.



Runnble is an interface but in this code is instanciated with new , how is this posible??

I´m lost with this code. Can anybody  help me to me  explaining this code, why you can instantiate the Runnable interface?
Sorry for my bad english...
Cheer.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is called a anonymous class,here it is not instantiating the interface(cannot instantiate ever).anonymous class is a local class without name.so what actually going on here is "new" means instantiate a unnamed class and after that runnable() which means let that unnamed class implement runnable and after that it is overriding run method in a body of unnamed class(of type runnable).

Hope this will help!

Kind regards,
Praveen.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to ranch Sergio!
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read oracle tutorial for better understanding anonymous class definition.
 
Sergio Barcelona
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok , runnable is an anonimus class ,This concept is too much for me right now...
Thanks for the reply ,now it continue studying java to understand it well in the future.
Cheers
 
Stephan van Hulst
Saloon Keeper
Posts: 7817
142
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch, Sergio!

No, Runnable is not an anonymous class. Runnable is an interface. However, you can create an instance of an anonymous class that implements the interface, by using the new operator on the interface name, and then providing an implementation between curly braces.
 
Dave Tolls
Rancher
Posts: 2914
36
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In fact, if you look in the directory where the .class files have been compiled to you will see a class called something like AnonymousInnerClassTest$1.class.
That is the class the compiler has created to represent the anonymous class.
 
Sergio Barcelona
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I understand the concept of anonymous class that implements one interface.
Thanks for responses.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!