Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

interfaces  RSS feed

 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interfaces cannot be instanciated, right?
and so since Runnable is an interface that means you can not write
Runnable r = new Runnable();
correct?
 
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's correct...
Interface are meant to be implemented by classes. For instance, the Thread class implements the Runnable interface so you could write:
Runnable r = new Thread(); //because a thread is_a Runnable too.
as well as
Thread t = new Thread();
 
Amir Ghahrai
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx Valentin,
The reason I asked the above question was that because I read the following link which confused me for a while
http://www.javaworld.com/javaworld/javaqa/2000-03/04-qa-0331-cert.html?
take a look at question 3. and it says the correct answers are A and C. but A should not work.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No the answer is correct because if you look at the code carefully an anonymous class is declared:

You can notice that after "Runnable()" there is no semicolon but an opening brace which tells the compiler "Now we provide an inline implementation for the Runnable interface".
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!