Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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".
 
The knights of nee want a shrubbery. And a tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!