• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

INterface should have implemetation

 
sanjey asok
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 180
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15205
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic