Advanced Packages and Access Modifiers in Java: Example 3

Rumman Ansari   Software Engineer   2024-07-05 03:35:43   6746  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

Table of Content:

Within Package and Sub Package (default method)

We are trying to access a default method Within Same Package but from sub package different class

Sub Package default method Save file: ClassA.java
package pack1;

class ClassA{

  void MethodOfClassA()
 	{
 	 System.out.println("Method of ClassA");
	}

}
Save file: ClassB.java
package pack1.subpack1;
import pack1.ClassA;

class ClassB{

  public static void main(String args[])
  {
 	ClassA object = new ClassA();
    object.MethodOfClassA();
  }

}
 

C:\Users\Hello World>E: 

E:\>javac -d . ClassA.java

E:\>javac -d . ClassB.java
ClassB.java:3: error: ClassA is not public in pack1; cannot be accessed from outside package
import pack1.ClassA;
            ^
ClassB.java:9: error: cannot find symbol
        ClassA object = new ClassA();
        ^
  symbol:   class ClassA
  location: class ClassB
ClassB.java:9: error: cannot find symbol
        ClassA object = new ClassA();
                            ^
  symbol:   class ClassA
  location: class ClassB
3 errors
  
 

Within Package and Sub Package (public method)

We are trying to access a public method Within Same Package but from sub package different class

Sub Package public Method Save file: ClassA.java
package pack1;

public class ClassA{  // must be public

  public void MethodOfClassA()
 	{
 	 System.out.println("Method of ClassA");
	}
 
}
Save file: ClassB.java
package pack1.subpack1;

import pack1.ClassA;

class ClassB{

	public static void main(String args[])
	{
	ClassA object = new ClassA();
    object.MethodOfClassA();
    }
}
  

C:\Users\Hello World>E:
 
E:\>javac -d . ClassA.java

E:\>javac -d . ClassB.java

E:\>java pack1.subpack1.ClassB
Method of ClassA
 
 

Within Package and Sub Package (protected method)

We are trying to access a protected method Within Same Package but from sub package different class

Sub package Protected Method Save file: ClassA.java
package pack1;

class ClassA{

   protected void MethodOfClassA()
 	{
 	 System.out.println("Method of ClassA");
	}

}
Save file: ClassB.java
package pack1.subpack1;

import pack1.ClassA;

class ClassB{

	public static void main(String args[])
	{
	ClassA object = new ClassA();
    object.MethodOfClassA();
    }
}
  

C:\Users\Hello World>E:
  
E:\>javac -d . ClassA.java

E:\>javac -d . ClassB.java
ClassB.java:3: error: ClassA is not public in pack1; cannot be accessed from outside package
import pack1.ClassA;
            ^
ClassB.java:9: error: cannot find symbol
        ClassA object = new ClassA();
        ^
  symbol:   class ClassA
  location: class ClassB
ClassB.java:9: error: cannot find symbol
        ClassA object = new ClassA();
                            ^
  symbol:   class ClassA
  location: class ClassB
3 errors
 
 

Within Package and Sub Package (private method)

We are trying to access a private method Within Same Package but from sub package different class

if we change the class from private to public also the program will not run, to rum the program MethodOfClassA should be public

Sub Package private Method Save file: ClassA.java
package pack1;

public class ClassA{

   private void MethodOfClassA()
 	{
 	 System.out.println("Method of ClassA");
	}

}
Save file: ClassB.java
package pack1.subpack1;

import pack1.ClassA;

class ClassB{

	public static void main(String args[])
	{
	ClassA object = new ClassA();
    object.MethodOfClassA();
    }
}
   

E:\>javac -d . ClassA.java

E:\>javac -d . ClassB.java
ClassB.java:10: error: MethodOfClassA() has private access in ClassA
    object.MethodOfClassA();
          ^
1 error

  

Within Package and Sub Package (private method)

We are trying to access a private method Within Same Package but from sub package different class

ClassA is not public in pack1; cannot be accessed from outside package

MethodOfClassA() has private access in ClassA. It should not be private, it should be public to run.

Sub Package private Method Save file: ClassA.java
package pack1;

class ClassA{

   private void MethodOfClassA()
 	{
 	 System.out.println("Method of ClassA");
	}

}
Save file: ClassB.java
package pack1.subpack1;

import pack1.ClassA;

class ClassB{

	public static void main(String args[])
	{
	ClassA object = new ClassA();
    object.MethodOfClassA();
    }
}
   
E:\>javac -d . ClassA.java

E:\>javac -d . ClassB.java
ClassB.java:3: error: ClassA is not public in pack1; cannot be accessed from outside package 
import pack1.ClassA;
            ^
ClassB.java:9: error: cannot find symbol
        ClassA object = new ClassA();
        ^
  symbol:   class ClassA
  location: class ClassB
ClassB.java:9: error: cannot find symbol
        ClassA object = new ClassA();
                            ^
  symbol:   class ClassA
  location: class ClassB
3 errors