Access Control of Classes in SAME Package

  1. Main function of a Package:

  2. Recall the following access modifiers:

    Example: private & public modifiers
          public class BankAccount
          {
    	 private double balance;
    
    	 public void deposit(double amount)    
    	 {
    	     ....
    	 }
          }
    
    

  3. The Package Access permission:

    Example: package access permission
          public class MyClass
          {
             int myVariable;
    
             void myMethod(...)     
             {
    	    .....
             }
          }
    
    

  4. Package access rule:

    
        package P1;
    
        public class myClass1     
        {
           int myVar1;
    
           void myMethod1()
           {
    	 ...
           }
        }
    
    

    
        package P2;
    
        public class myClass2     
        {
           int myVar2;
    
           void myMethod2()
           {
    	 ...
           }
        }
    
    

    
        package P1;
    
        public class Test
        {
           void TestMethod
           {
    	 // Can access myVar1 & myMethod1
    	 // in class myClass1
    
    	 // CANNOT access myVar2 & myMethod2   
    	 // in class myClass2
           }
        }