Packages and Access Modifier in Java

Package is a logical grouping of classes under a common namespace.

So far all our code have this package declaration:

package com.java;

That means that inside our project, we have a folder under ‘src’ with the name ‘com’.

And further inside ‘com’ we have another folder with the name ‘java’. So far all our classes resides in this ‘java’ folder.

The above statement is called package declaration. Only one rule you have to keep in mind:

The package declaration must be the very first statement inside your file, otherwise you will get a compilation error.

Java does not allow you to create more than one class with the same name inside any package. That’s why, so far we have created classes with different names, and otherwise we would get a compilation error.

Depending upon the access modifier used, you can restrict or allows access of class member inside or outside the package the class is in.

I can perfectly guess that after reading the above statement, you are thinking: what is this supposed to even mean?

We need to know what access modifier at first place is.

Access modifier is some set of keywords in java, which is used to define member access type that is where in our code we can access the member and where not.

Remember again, that class member is both variables and method. So both can have access modifier.

Java has three main access modifier, namely: public, private and protected.

We have already used public and private in our code, but protected is new to us. All three are keywords in java.

Consider the below three class. And now create a new package ‘example’ under ‘com’. And in that package create this below class.

package com.java;

class Superclass {

	public int p = 10;
	private int q = 15;
	protected int r = 20;
	int s = 30;

	protected void methodA() {
		System.out.println("in super class protected method");
	}

	private void methodB() {
		System.out.println("in super class private method");
	}

}

package com.java;

class SamePackageSubClass extends Superclass {

	int abc1 = p + 10;
	int abc2 = q + 10;
	int abc3 = r + 20;
	int abc4 = s + 30;

	public static void main(String[] args) {

		SamePackageSubClass object1 = new SamePackageSubClass();
		object1.methodA();
		object1.methodB();

	}
}

package com.example;

public class DifferentPackageSubClass extends Superclass {

	int abc1 = p + 10;
	int abc2 = q + 10;
	int abc3 = r + 20;
	int abc4 = s + 30;

	public static void main(String[] args) {

		DifferentPackageSubClass object = new DifferentPackageSubClass();
		object.methodA();
		object.methodB();
	}

}

Errors:

DifferentPackageSubClass.java:27: error: q has private access in Superclass
int abc2 = q + 10;
^
DifferentPackageSubClass.java:35: error: cannot find symbol
object1.methodB();
^
symbol: method methodB()
location: variable object1 of type SamePackageSubClass
DifferentPackageSubClass.java:47: error: q has private access in Superclass
int abc2 = q + 10;
^
DifferentPackageSubClass.java:55: error: cannot find symbol
object.methodB();
^
symbol: method methodB()
location: variable object of type DifferentPackageSubClass
4 errors

In the first class Superclass we have defined four integer variables with four different access modifier; public, private, protected and default. If you don’t mention any modifier, then it is default modifier, which is for variable ‘s’.

Also, we have two methods, one with private access modifier and another with protected access modifier.

Now in the same package (com.java) we have created a subclass SamePackageSubClass.

You will get compilation error in this two line:

int abc2 = q + 10;

object1.methodB();

because, the variable ‘q’ and ‘methodB’ both are defined as private in the ‘Superclass’.

Now, check the code in DifferentPackageSubClass. Here you will get compilation error in these three lines:

int abc2 = q + 10; // because ‘q’ is defined as private
int abc4 = s + 30; // because ‘s’ is defined as default and default member is not accessible in different package.
object.methodB(); // because methodB is defined as private

 While it may seem confusing, the below table summarizes the rules:


Next > < Prev
Scroll to Top