This week's book giveaway is in the Java in General forum.
We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line!
See this thread for details.
Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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: 22579
122
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: 22579
122
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
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic