My apologies for dumping such a lengthy code here.
I completed chapter one of OCA Study Guide I J. Boyarsky and S. Selikoff and took chapter one review questions. Question 7 answer is not clear to me. The question is below.
Given the following classes, which of the following snippets can be inserted in place of INSERT IMPORTS HERE and have the code compile? (Choose all that apply)
A. import aquarium.*;
B. import acquarium.Water; import acquarium.jellies.*;
C. import aquarium.*; import acquarium.jellies.Water;
D. import aquarium.*; import acquarium.jellies.*;
E. import aquarium.Water; import acquarium.jellies.Water;
F. None of these imports can make the code compile.
My Answer: I chose A. The book said it is A, B, C.
My Questions & confusion: Why are B and C included in the answer? I know that the package acquarium contains Water and Jellies classes and possibly employees classes. Hence the wild card used in choice A makes sense.
In choice B a wild card is used after the jellies class (import acquarium.jellies.*;), isn't that an attempt at importing a method which is wrong?
In choice C, can you import two classes in one import (import acquarium.jellies.Water;)? If File and Paths are in the same package java.nio.file and your options are: import java.nio.file.*, import java.nio.file.Files; ,import java.nio.file.Paths but you cannot do import java.nio.file.Files.Paths, so why is this acceptable in choice C above?
For all those thinking why on earth anything should be filled in, the excersize is as follows (at least in my book):
The rules are: if there is only one import of the class Water, then that is fine. So. option A is correct. If there are two imports of the class Water, then the import that is most specific is chosen. This is the case for options B and C, where the import ...Water is more specific than import ...*
If the imports are equally specific, a compiler error is generated, i.e. for options D and E. So, the book is correct.
As you can see, the Water class in that code refers to the aquarium.Water which is the first of the three classes in the question.
As you said, wildcards don't include subdirectories, and as you also believe (correctly), aquarium.jellies IS a subdirectory. So those two things imply the import statement there does not import aquarium.jellies. Therefore the import statement in answer A's code only refers to one version of the Water class and therefore it compiles successfully.
So I dunno... everything you thought was correct was indeed correct, but somehow your thought process went off the rails after that. Anyway, does that clarify things for you?
posted 1 year ago
Thanks Paul, I get it now.
For some reason my brain wanted imports for both classes.