This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes INterface should have implemetation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "INterface should have implemetation " Watch "INterface should have implemetation " New topic
Author

INterface should have implemetation

sanjey asok
Ranch Hand

Joined: Jul 11, 2009
Posts: 39


here ExecutorService is an interface and Executors.newFixedThreadPool(NTHREDS); return a n intreface ,how it is possible?,an interface should have implementation class ?
saurav sarkar
Ranch Hand

Joined: Jan 07, 2007
Posts: 180

Hi Sanjey,

Here actually the type of reference is an interface but it is actually
poniting to an implementation of ExecutorService.

The method Executors.newFixedThreadPool(NTHREDS); actually
returns an implementation of ExecutorService.

cheers,
Saurav

Be Objectively Oriented.Explore the power of OOPs.
My Blog, Eclipse EMF Query committer.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

It's perfectly ok to make a variable of a type that is an interface refer to an object of a class that implements the interface. For example, you can write:

Note that List is an interface, and ArrayList is a class that implements the List interface. The rest of the program, that uses list, only needs to know that it is an object of a class that implements List; it doesn't need to know that it's actually an ArrayList (or some other implementation such as LinkedList).

Likewise, the method Executors.newFixedThreadPool returns an object that's an instance of a class that implements the ExecutorService interface. You don't need to know what that exact class is to be able to use the ExecutorService.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
Consider Paul's rocket mass heater.
 
subject: INterface should have implemetation
 
Similar Threads
Callable implementation
java synchronized- Account Update
How to wait for multiple thread to finish run
Question in Callable interface
Using ExecutorService instead of regular Threads