Powered by Blogger.

Access Protection

No comments :
The three access modifiers, private, public, and protected provide a variety of way to Process the many levels of access required by these categories. while java's access control mechanism may seem complicated, we can simplify it as follow. Anything declared public can be accessed from anywhere. Anything declared private can not be seen outside of its class. when a member does not have an explicit access specification, it is visible to subclasses as well as to other class in the same package. this is the default access. if you want to allow an element to be seen outside your current package, but only to classes that subclass your class directly, then declare that element protected.
Class Member Access Table:
Private No Modifier Protected Public
Same class Yes Yes Yes Yes
Same package subclass No Yes Yes Yes
Same package non-subclass No Yes Yes Yes
Different package subclass No No Yes Yes
Different package non-subclass No No No Yes

Here, Table applies only to members of classes. A non-nested class has only two possible access levels: default and public. when a class is declared as public, it is accessible by any other code. if a class has default access, then it can only be accessed by other code within its same package. when a class is public, it must be the only public class declared in the file, and the must have the same name as the class.

An Access Example: It has two package and five classes. Remember that the class for the two different packages need be stored in directories named after there respective package- in this case, p1 and p2
The source for the first package define three package: Protection, Derived and samePackage. The first class defines four int variables in each of the legal protection modes. The n is declared with the default protection, n_pri is private, n_pro is protected and n_pub is public.
The second class, Derived, is a subclass of Protection in the same package, p1. This grants Derived access to every variable in Protection except for n_pri, the private one. The third class, samePackage, is not a subclass of Protection, but is in the same package and also has access to all but n_pri.
This is file Protection.java

Following is the source code for the other package,p2. The two classes defined in p2 cover the other two conditions that are affected by access control. The first class, protection2, is a subclass of p1.Protection. This grants access to all of p1.protection's variables except for n_pir (because it is private) and n, The variable declared with the default protection. Remember, the default only allows within the class or the package, not extra-package subclass. Finally, the class otherPackage has access to only one variable, n_pub, which was declared public.
This is file protection2.java

If you want to try these two packages, here are two test files (runp1package and runp2package) you can use. The one package p1 is show here:

The one package p2 is show here:

No comments :

Post a Comment

Please Write a Message for Programming or something Related issues.