1

I'm working in a project which contains the following interface

import ListIF;

public interface PlayListIF {

    public ListIF<Integer> getPlayList();

    public void addListOfTunes(ListIF<Integer> lT);

    public void removeTune(int tuneID);

}

later on, another classes use

import PlayListIF 
import PlayList

and later making an instace:

PlayListIF playlist = new PlayList();

Is there any differences between importing an interface or its implementation? What is the point of importing the interface?

Thanks

Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
  • 1
    I'm confused. Are you asking what an `import` declaration does or are you asking why you're allowed to write `PlayListIF playlist = new PlayList();`? – Sotirios Delimanolis Jun 27 '17 at 19:42
  • The import statement just allows you to refer to the interface without fully qualifying its name with the package. You're using the unnamed package, so you don't have to qualify it anyway. However, note that there are [issues associated with using the default package](https://stackoverflow.com/questions/7849421). – Andy Thomas Jun 27 '17 at 19:44
  • Any type which you ever refer to, interface, class, or enum, generally needs to be imported (unless you explicitly qualify it everywhere or it lives in the same package). – Louis Wasserman Jun 27 '17 at 19:58
  • 1
    Java basics, any tutorial will help – UninformedUser Jun 27 '17 at 20:25

3 Answers3

1

The point of importing an implementation (PlayList) and the interface is because the line of code,

PlayListIF playlist = new PlayList();

references both an instance of an implementation and the interface. The variable playlist could have been any instance of a class that implements the PlayListIF. It just so happens to be an instance of the PlayList class when it could be an instance of any class that implements the PlayListIF interface.

kzimmerman
  • 49
  • 3
  • 1
    You can't import an instance. You can, however, import a class. Note also that the variable is not an instance -- it's a reference to an instance. – Andy Thomas Jun 27 '17 at 19:48
  • Yes, thank you. I was lazy in my words and it was technically incorrect. I replaced the word instance with implementation where appropriate. – kzimmerman Jun 27 '17 at 20:09
  • 2
    In addition, the actual point to importing is not having to fully qualify the class and interface names. – Andy Thomas Jun 27 '17 at 20:13
-1

Any class/interface that you refer in your code needs to be imported, so that the compiler knows what you are referring to. In this case you are referring to both, so you need to import both.

Rohan Shetty
  • 227
  • 2
  • 9
  • 3
    Not true. You can refer to classes/interfaces without importing, if you use their fully-qualified name, or import their whole package. – Andy Thomas Jun 27 '17 at 19:51
-1

I can try to explain by giving a small example for a newcomer in java. Having a class that have a method save(something). You might want to save the "something"

  1. Your hard drive
  2. an FTP
  3. through a web service
  4. etc...

Basically you would write a service (interface)

public interface Saver{    
    public void save(MyData something);
}

And you would have any implementation of this like

  • FTPSaver
  • HardDriveSaver
  • etc...

Now when you actually USE your Saver lets say

public class A {
    private Saver saver;
    public A (Saver saver){
        this.saver = saver;}
}

You can inject through the constructor FTPSaver or HardDriveSaver depending on how you want it to save in that particular context but for Class A it is using a Saver it does not matter which one.

TLDR: Interface are a contract it shows available method regardless of their implementation if you implement an interface you MUST implement all method in it. And you should use the interface as attribute not the implementation.

Hope it helped

drgn
  • 1,080
  • 1
  • 11
  • 21
  • 2
    What does this have to do with the user's question about **importing**? – Andy Thomas Jun 27 '17 at 19:56
  • The user's question lets me question myself on his knowledge on interface. It does not specifically answer the "import" question. By explaining it a little bit more in depth i though he would understand by himself that he can just import the interface. and inject an implementation. My apologies if i was not clear enough – drgn Jun 27 '17 at 19:58