• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

interfaces

 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
reply
    Bookmark Topic Watch Topic
  • New Topic