• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

Question about packages.

 
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Fellow Ranchers,
I'm having an issue with knowing how class access works when classes are in different packages.
The question I have is:
If I have an interface in one package(app.pack1), and that interface is public:

And I have a class that implements the Interface in another package(app.pack2).

Why do I have to import it(import app.pack1.MyInterface...) If the interface is public?

Could someone please explain to me these basics about packages?

Thank in advance,

Sincerely,
Jose
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider this for example, you want to use class ArrayList that is located in the package java.util if you didnt specify with import the package to search then the Compiler and JVM would have to search the entire packages which would take long time to find the appropriate class.

So using import statements you tell the Compiler and JVM where to find classes.
[ October 01, 2007: Message edited by: Ahmed Yehia ]
 
Sheriff
Posts: 22743
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another fine example: List.

Now, which one would you like? java.awt.List (a GUI element) or java.util.List (the interface)? Without explicitly importing it (with or without wildcards), the compiler doesn't know which one you need.
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello
I've read the responses, and I have to say I'm very thankful.
So, I understand now that classes or interfaces that will be used or implemented in a class must be imported or the compiler will not know which one to use. and this happens when classes or interfaces are in different packages.

So, Do the same rules apply when everything is on the same package? or how does it work in that case?

As always, keep up the fantastic work,
and god bless you all
Jose
 
Rob Spoor
Sheriff
Posts: 22743
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jose Campana:
So, Do the same rules apply when everything is on the same package? or how does it work in that case?


Classes and interfaces from two locations are automatically imported, and do not need to be explicitly imported:
- the same package
- the java.lang package

Of course you still can import them, but you don't have to.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jose Campana:
Hello Fellow Ranchers,
I'm having an issue with knowing how class access works when classes are in different packages.
The question I have is:
If I have an interface in one package(app.pack1), and that interface is public:

And I have a class that implements the Interface in another package(app.pack2).

Why do I have to import it(import app.pack1.MyInterface...) If the interface is public?

Could someone please explain to me these basics about packages?

Thank in advance,

Sincerely,
Jose




It is not necessary to use import in order to use an interface or class from a different package. One can use a public class or an interface from any package by using the fully qualified class name.
So in a java file an import statement is simply a declaration of a shortcut
to indicate that you would like to use the class name alone instead of the
fully qualified class name through out a given java file.
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mr. Zen of Java Programming, thank you very much...

This will help me in my java training.

God bless.

sincerely, Jose
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic