File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes package & protected Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "package & protected" Watch "package & protected" New topic

package & protected

E Lan

Joined: Mar 15, 2004
Posts: 11
The following is one example in my book,I am not really understand concept of package and protected.

package1--A protected static int p1;
protected static p2();
protected p3();

--B extends A


package2--D extends A


Above situation is the classes(A,B,C,D,E) arrange in packages(package1 and package2).
We add below programs to all contructors of classes B,C,D,and E.

A a=new A();
a.p1=1; //line1
a.p2(); //line2
A.p2(); //line3
a.p3; //line4

Of course we have imported package1.A to classes D and E.

When compile B,B and A are in the same package,and B inherited A,so compiling B is ok.
C is not inherited A,why does compile C ok ,too?
Althought C and A are in the same package,but C is not inherited A,is that ok to use A class in C?

In my opinion,althought classes are not in the same package,but they have inheritance relation,then the subclass can create and use another class.
may I wrong?
That's the same problem about D in the above example.
After compiling D,line1 and line4 have errors.Why do this two lines have errors?
Pease explain generally.

Thanks a lot to answer my fuzzy question.

[ June 14, 2004: Message edited by: E Lan ]
[ June 14, 2004: Message edited by: E Lan ]
I agree. Here's the link:
subject: package & protected
It's not a secret anymore!