Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

import an interface?

 
Carol Murphy
village idiot
Bartender
Posts: 1203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm looking over PathFinder.java, one of the classes included in jr.jar. I just discovered that the import statement in this class is actually importing an interface, not a class. When you import an interface, do you still have to implement it? I just read that the container is responsible for implementing this particular interface, but I was only aware that interfaces were implemented, not imported! If you do import a different interface in some other class, would you then have to implement it, or how would that work?
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, when you import an interface, you DO NOT have to impelment it. On the other hand, if you implement an interface, you DO have to import it if the interface is declared in a different package than the class that implements it.

To help you understand this, the import statement simply tells the current file which classes and/or interfaces are available to be used. (Okay, there is a little more to it than this, but I'm trying to keep it simple.) Or in other words, whenever you USE a class or interface that belongs to a different package, you MUST import it. It makes no difference whether you use the class or interface as a local variable, member variable, or if you extend a class or implement an interface. In each case, you need to tell the compiler where to find the class or interface. This is what the import statement is for.

Layne
 
Carol Murphy
village idiot
Bartender
Posts: 1203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmmm. That's very interesting! Thanks Layne!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic