Codingtree

As we know a file name in Java should be same as that of a class name, so it means that there cannot be two classes with the same name in the entire project. When a Java application grows big, it would be difficult to maintain all the class/Java files that are present in the project. Accordingly, it would be nice to have a logical separation of classes depending on the module. This logical separation of name space is called a package. A package in Java is created with help of a keyword "package". We will see an example program that creates a package in the next section.

Access specifiers

We have seen access specifiers used though out the module, right from the first chapter, now that we are introduced to the concept of package it makes sense to talk about them in detail. So far, we have seen the usage of private, public and no access specifier, in this section we see a new type of access specifier called "protected". Let us understand the scope of each of these specifiers.



Figure: Access Specifiers

The concept of package adds a new logical separation and access restrictions are achieved using the appropriate access specifier.

Let us look at access specifiers restrictions for various Java elements. A class can have either "public or no access specifier"; private and protected specifiers are not allowed in-front of the key word "class". Variables, constructors, methods are allowed to have, any of the above mentioned access specifiers.

Let us look at a sample program that demonstrates the usage of packages.

 Package Name:package1,  File Name:Package1Class.java
 -----------------------------------------------------

 /* Creating a package using the keyword "package" */
 package package1;

 /* 
  * Import the package (That contains the class) first, this is required 
  * if we were to create an object/Access member-variables of a class (Package2Class). 
  */
 import package2.Package2Class;

 public class Package1Class {
     public void p1Method1() {
         Package2Class p2Obj = new Package2Class();
         System.out.println(" Inside Package 1, p1Method1");

         /* Methods with "default access specifier" are not visible
          * Outside its package. The below statement throws an error.
          */
         //p2Obj.p2Method1();
     }
 }

 Package Name:package2,  File Name:Package2Class.java
 -----------------------------------------------------
 /* Creating a package using the keyword "package" */
 package package2;

 import package1.Package1Class;

 public class Package2Class {
     Package1Class p1Obj = new Package1Class();
     protected int p2var1;

     void p2Method1() {
         System.out.println (" Inside Package 2, Method1");
         /* Methods with "public access specifier" are visible
          * thought out. The below statement doesn't throws an error.
          */
         p1Obj.p1Method1();
     }

     // Protected Access specifier, this method is visible inside a package.
     protected void p2Method2() {
         System.out.println(" This is protected method inside Package 2");
     }

     public static void main(String[] args) {
         Package2Class p2Obj = new Package2Class();
         Package2Class2 p2c2Obj = new Package2Class2();

         System.out.println (" Main Method ");
         p2Obj.p2Method1();
         p2c2Obj.p2Method2();
     }
 }

 Package Name:package2,  File Name:Package2Class2.java
 -----------------------------------------------------
 package package2;

 public class Package2Class2 {
     void p2Method2() {
         Package2Class p2Obj = new Package2Class();
         System.out.println (" Inside Package 2, Method2");
         /* Methods with "Protected access specifier" are visible
          * thought out the package. The below statement does not throws an error.
          */

         // Protected members are accessible inside a package.
         p2Obj.p2Method2();
     }
 }

Here is the output, demonstrating the concept of packages and access specifiers.

  Main Method 
  Inside Package 2, Method1
  Inside Package 1, p1Method1
  Inside Package 2, Method2
  This is protected method inside Package 2




comments powered by Disqus