I would like to ask you if I understand the review question correctly:
Given the following classes,, which of the following can independently replace INSERT IMPORTS HERE to make the code compile? (Choose all that apply)
A) import aquarium.*;
B) import aquarium.*.Jelly;
C) import aquarium.jellies Jelly;
D) import aquarium.jellies.*;
E) import aquarium.jellies.Jelly.*;
THE CORRECT ANSWER IS:C,D
The question is to choose all options to make the code compile.
I understand why option D is a correct answer. If I import import aquarium.jellies.*; - it has both classes inside Tank & Jelly. And when I import this I don't need anything else, the code will compile. Clear.
Now when it comes to option C.
If I import drectly the child class - import aquarium.jellies Jelly; - With just this package import will the code compile? Forget correct answer D. Wll it compile because it is in hierarcy so even thouth I import a specific child class Jelly, it compiles becausse it has the aquarium package, under whch is class Water? Or will it not compile?
Ingrid Nemeckova wrote:If I import import aquarium.jellies.*; - it has both classes inside Tank & Jelly. And when I import this I don't need anything else, the code will compile. Clear.
You are incorrect! The import aquarium.jellies.* will only import all classes in the package aquarium.jellies, so in this example that's only class Jelly. Nothing else! And please keep in mind that the wildcard * is not recursive, meaning it will only import the classes from that package, not the classes in any subpackage of this package.
So does this correction makes any difference to your understanding why answer D is correct?
PS. Always use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information. Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers. I already added the code tags for you. See how much easier the code is to read?
Ingrid Nemeckova wrote:I don't even need the package aquarium cause the package visitor doesn't need that since it only works with class Jelly, therefore I need to import a package wich has the class Jelly inside. OMG!!!
Exactly! You are spot-on!
And to be 100% accurate: you never import a package! You import a single class (e.g. import aquarium.jellies.Jelly;) or all classes within a package (e.g. import aquarium.jellies.*;). So to make sure that the AquariumVisitor class will successfully compile you need an import that will import the Jelly class.
Thank you very much for this explanation. The packages and imports was the one thing I really didn't understand from chapter 1 and this just made it crystal clear.
Plus learning to understand the question correctly is a another entire task for me :-)