Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

OCA - Chapter 1 - Revew question 5 - Packages

 
Greenhorn
Posts: 3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,

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.


But

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?

Thank you.

 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OH....! and I thought it will import it all! All classes under aquarium and all classes under aquarium.jellies. *Face palm*

omg I get the answer now! OMG I completely missed what the test was asking me about ROFL.

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!!!

humpfff, thank you. That was mind opening!!!
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Ingrid Nemeckova
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 :-)
 
Slime does not pay. Always keep your tiny ad dry.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic