Reputation: 14139
If I want to use an ArrayList
in my code, I need to import java.util.ArrayList
. If I also want to use a List
, I have to import java.util.List
. Why is this needed? ArrayList
inherits from List
, and thus uses code from it. Why does that not carry over to my class? Surely if ArrayList
imports List
, and my class in turn imports ArrayList
, then List
should be defined in my class?
Upvotes: 0
Views: 1829
Reputation: 2377
Yes and no. An ArrayList object will indeed have access to any property (method, object...) defined in the class it extends/implements. But, the class you are working in doesn't know about such super classes. You have to import them explicitly to reference them. You could of course import java.util.*
and that will give your class access to all classes in that package.
Upvotes: 0
Reputation: 322
I completely agree with your question. It would be a lot easier to write code if we do not have to worry about keeping track of long list of import statements.
But, the way java (compiler and runtime) works, I think it is necessary to do so because of the following.
The import statements in Java are only for a compiler. The runtime doesn't know anything about import statements.
When you compile your code, all the class names that you use gets converted into fully qualified classnames by the compiler. If we explicitly do not mention which class we are referring to, compiler might get confused if there are multiple classes matching with the same class name that you provided.
You could say that, java should atleast import non-duplicate classes automatically. But the problem is you never know which classes are non duplicate.
Anyone can create a class with any name (I can even create a class in my own package with name ArrayList or List). For that reason, compiler need to know beforehand if it has to resolve List to java.util.List or some other List.
Upvotes: 0
Reputation: 4023
This is a clear instance of "declare what is being used". You might consider is a distant variant of "need to know".
Using an "ArrayList" does not require you to use "List" also. If, however, you want to use a "java.util.List" (e,g, as the type of a variable being initialized with an instance of "ArrayList"), then you clearly should state that you are using both types. (Or actually , just disambiguate what is intended to be referred to by the simple names used, like "List".)
Why clutter your import section with any possible parent type and interface from any class you are using (even if done implicitly)? Would likely not provide too much benefit, while at the same time it would make live harder in case you reall want to use a different class with the same name (other package).
Of course you might remember that you are not required to use "imports" at all. You will need to use full names of classes then, but it is completely valid to do so.
Furthermore, most modern IDEs will suggest a proper import when using a simple name based on what is also used in the expression context. (So e.g. if you use "List" as a variable type when assigning from an "ArrayList" might suggest "java.util.List" as resulution for "List" with higher priority than other clases named "List".
Upvotes: 0
Reputation: 111369
The import
keyword does not actually "import" anything: it just tells the compiler that you would like to refer to something by its short name. This is why the "import List" in the ArrayList class has no effect on your class.
Upvotes: 1
Reputation: 888107
import
is just a syntactic feature that lets you avoid writing out the full package every time you use a classname.
You do not need to import dependencies at all.
Upvotes: 5
Reputation: 403
You can import everything from a particular package like so: java.util.*;
Keep in mind though, that there is a particular annoyance when doing so, as you need to fully qualify other objects that may share the same class name with the imported package. Check this for reference:
Why is using a wild card with a Java import statement bad?
Upvotes: 0
Reputation: 533820
Some classes depend on a lot of classes and those classes depend on a lot of classes, and you would end up importing a good portion on the JDK. It can't assume which classes/interface you might want to import as well.
If you use an IDE, it can add the imports for you so you don't have to worry about which ones are needed most of the time.
Upvotes: 2