• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using methods of an Interface  RSS feed

 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have following code. In this code CSClient is an interface. All methods of CSClient are implementaed in CSClientImpl class. Do I not need CSClientImpl imported in this code ?
How can I call getBranch() of CSClient, which is not implemented in CSClient as " this.getCsClient().getBranch(new CSVPath(vpath), true);" ?
This code works fine without any error in eclipse. So my confusion is, how can a method getBranch(), which is implemented in CSClientImpl class be used in this code without importing CSClientImpl ?Thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The import is only for the benefit of the compiler, which only sees (and only needs to see) the interface. The implementing class is irrelevant.
 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont understand. Do you mean to say, implementing class is not required to be imported ? If interface is imported then that will suffice to use impllementing class ?

How does it work ? When interface is imported then only those unimplemented methods are available for use and not the implemented ones.

Please elaborate more.
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nirjari patel wrote:I dont understand. Do you mean to say, implementing class is not required to be imported ? If interface is imported then that will suffice to use impllementing class ?


Yes. You may or may not know what class is going to implement the interface at runtime; the compiler certainly doesn't know that and hence doesn't need an import for that class.

How does it work ? When interface is imported then only those unimplemented methods are available for use and not the implemented ones.


I don't know what you mean by "implemented" and "unimplemented" methods. As far as the compiler is concerned, the interface declares a number of methods and all of those methods are available for variables of the interface's type to use. That's all there is to it as far as the compiler is concerned.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nirjari patel wrote:I dont understand. Do you mean to say, implementing class is not required to be imported ?

That is exactly what I (and Paul) are saying. The implementing class is irrelevant at compile time. Of course, it needs to be in the classpath at run time.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!