Ive created a system so far that works fine with the following structure "com.project;" I then added a sub section to that and moved some of the classes to it "com.project.sub;"

when the classes where originally in com.project; and I had there methods as protected I could access them fine but can no longer access them when they are in the sub-section.

From what Ive read protected allows access from other classes in the same package, If so what constitutes a package if I cant access them in the subsection with protected?

Posts:14,142 Registered: 99-04-02

Re: Packages and Access levels Aug 6, 2004 10:40 AM (reply 1 of 9)

com.project and com.project.sub are 2 totally separate packages, as far as Java is concerned. There's no real parent-child type relationship to packages. Since projected only allows access to things in the same package, it's only in that exact package. There's no inheritance there.

Posts:3,258 Registered: 00-08-28

Re: Packages and Access levels Aug 6, 2004 12:06 PM (reply 2 of 9)

Hi

Ive created a system so far that works fine with thefollowing structure "com.project;" I then added a subsection to that and moved some of the classes to it"com.project.sub;"

when the classes where originally in com.project; andI had there methods as protected I could access themfine but can no longer access them when they are inthe sub-section.

From what Ive read protected allows access from otherclasses in the same package, If so what constitutes apackage if I cant access them in the subsection withprotected?

I guess you are mixing up class inheritance with I donot know what in packages. As mentioned in the previous post there is no such thing as package inheritance. Protected classes are visible to all the classes in the same package and to the classes inheriting from this class i.e., the child classes. This has got nothing to do with packages.

Posts:37,103 Registered: 3/30/99

Re: Packages and Access levels Aug 6, 2004 12:33 PM (reply 3 of 9)

From what Ive read protected allows access from otherclasses in the same package, If so what constitutes apackage if I cant access them in the subsection withprotected?

From most widely accessible to most tightly restricted:

public: Accessible anywhere

protected: Accessible only inside the same class, in another class inside the same package, and through the "this" or "super" reference in a subclass.

<no modifier>: "Package" or "default" access. Accessible only in the same class or another class in the same package.

private: Accessible only inside the same class.

Posts:14,142 Registered: 99-04-02

Re: Packages and Access levels Aug 6, 2004 12:33 PM (reply 4 of 9)

It's also related to why you can't do this:

import java.awt.*;

to mean:

import java.awt.*;import java.awt.event.*;import java.awt.geom.*;...

You have to import each "sub-package" you need classes from.

Posts:19 Registered: 7/26/04

Re: Packages and Access levels Aug 7, 2004 3:42 AM (reply 5 of 9)

So a folder is a package and does not include any subfolders? I other words any classes in a sub folder must be public for classes in its parent folder to access? So everything under com.project would not be part of a single package called com.project?

I think I understand, but what if I wanted classes in a sub folder(package) only accessible by its parent package (folder above it) ie. no other classes can use the classes in the sub folder other than the folder it exists in? if that made sense?

Posts:37,103 Registered: 3/30/99

Re: Packages and Access levels Aug 7, 2004 11:09 AM (reply 6 of 9)

So a folder is a package and does not include anysubfolders? I other words any classes in a sub foldermust be public for classes in its parent folder toaccess? So everything under com.project would not bepart of a single package called com.project?

Correct. All the class files in the com/project directory are part of the com.project package. All the class files in com/project/foo are part of the com.project.foo package, which bears no relation to the com.project package as far as Java is concerend--it just so happens that their names start the same.

Now, of course you should only put classes in the com.project.foo package if they have some logical relation to the com.project package, but that's just for your own organization--Java knows nothing about it.

There's no concept of a subpackage in Java. Packages are not hierarchical, even though the directory structure that matches the

I think I understand, but what if I wanted classes ina sub folder(package) only accessible by its parentpackage (folder above it) ie. no other classes can usethe classes in the sub folder other than the folder itexists in? if that made sense?

Can't do it. See reply 3 for what kinds of access control you have available.

Posts:4,130 Registered: 3/31/99

Re: Packages and Access levels Aug 7, 2004 3:10 PM (reply 7 of 9)

There's no concept of a subpackage in Java. Packagesare not hierarchical, even though the directorystructure that matches the

I used to think that there's no concept of a subpackage, but the JLS says differently. http://java.sun.com/docs/books/jls/second_edition/html/packages.doc.html#60384

The JLS does use the term subpackage

Posts:826 Registered: 7/1/03

Re: Packages and Access levels Aug 7, 2004 3:38 PM (reply 8 of 9)

The JLS does use the term subpackage

Interesting but unfortunate, no?

I mean there's no semantic meaning to the term.

/k1

Posts:37,103 Registered: 3/30/99

Re: Packages and Access levels Aug 7, 2004 4:02 PM (reply 9 of 9)

I used to think that there's no concept of asubpackage, but the JLS says differently. http://java.sun.com/docs/books/jls/second_edition/htmlpackages.doc.html#60384

The JLS does use the term subpackage

Hmmm. Bummer. The way they use it isn't the way I meant it.

Whatever terminology you use, my main point holds: As far as the language is concerned, there's no association between package com.foo and com.foo.bar.