File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "INterface should have implemetation " Watch "INterface should have implemetation " New topic

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.


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: 15092

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 8 API documentation
I agree. Here's the link:
subject: INterface should have implemetation
jQuery in Action, 3rd edition