Packages

K

kelvSYC

If you put a class inside a package, can it access classes in its
parent package without an import statement (eg. class foo is in package
a and class bar is in a.subpackage - can bar access foo without an
import statement)?
 
B

Brad BARCLAY

kelvSYC said:
If you put a class inside a package, can it access classes in its
parent package without an import statement (eg. class foo is in package
a and class bar is in a.subpackage - can bar access foo without an
import statement)?

Yes, but only if you fully-qualify the classname. This is true for
classes in _any_ package (not just a parent package).

Brad BARCLAY
 
H

Harald Hein

kelvSYC said:
If you put a class inside a package, can it access classes in its
parent package without an import statement (eg. class foo is in
package a and class bar is in a.subpackage - can bar access foo
without an import statement)?

Packages don't have parent packages, there is no parent - subpackage
relation. The only thing the two packages

abc.def
abc.def.ghi

have in common are the first seven characters in the name.

Package names are mapped to file names in case the .class files and
resources are stored in a file system. In this case it happens by
"accident" that the abc\def directory becomes the parent directory for
abc\def\ghi. Still, the packages are not logically related.

You can access public classes of another package by importing the other
package, or by specifying the full package name.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top