Package and Access Specifiers Example

This example shows how different elements of classes can be accessed under the control of the access specifiers (public, private, protected and default).

There are two sub-directories named a1  and a2  under the current directory C:\access.

 

For sub-directory a1

In sub-directory a1, we are having four programs named Base.java, Derived.java, SamePackage.java, and Main.java (Main class that contains the main() method). They are given below one-by-one.

Base.java

package a1;

public class Base {

int a_default = 10;
private int b_private = 20;
protected int c_protected = 30;
public int d_public = 40;

public Base() {
System.out.println("Base class constructor");
System.out.println("a = " + a_default);
System.out.println("b = " + b_private);
System.out.println("c = " + c_protected);
System.out.println("d = " + d_public);
}

}

 

Derived.java

package a1;

public class Derived extends Base {

public Derived() {
System.out.println("Derived class constructor");
System.out.println("a = " + a_default);
//System.out.println("b = " + b_private);
System.out.println("c = " + c_protected);
System.out.println("d = " + d_public);
}

}

 

SamePackage.java

package a1;

public class SamePackage {

public SamePackage() {
System.out.println("Non-subclass constructor");
Base ob = new Base();
System.out.println("ob.a = " + ob.a_default);
//System.out.println("ob.b = " + ob.b_private);
System.out.println("ob.c = " + ob.c_protected);
System.out.println("ob.d = " + ob.d_public);
}

}

 

Main.java

package a1;

public class Main {

public static void main(String[] args) {
Base ob1 = new Base();
Derived ob2 = new Derived();
SamePackage ob3 = new SamePackage();
}

}

 

We perform the following steps in order to compile the four programs from command prompt.

C:\>set classpath=C:\access

C:\>cd access

C:\access>cd a1

C:\access\a1>javac *.java

 

This is how we run the main program of a1 from command prompt.

C:\access\a1>cd ..

C:\access>java a1.Main
Base class constructor
a = 10
b = 20
c = 30
d = 40
Base class constructor
a = 10
b = 20
c = 30
d = 40
Derived class constructor
a = 10
c = 30
d = 40
Non-subclass constructor
Base class constructor
a = 10
b = 20
c = 30
d = 40
ob.a = 10
ob.c = 30
ob.d = 40

 

 

For sub-directory a2

In sub-directory a2 under the current directory “C:\access, we are having three programs named Derived2.java, DifferentPackage.java, and Main.java (Main class that contains the main() method). They are given below one-by-one.

Derived2.java

package a2;

import a1.Base;

public class Derived2 extends Base {

public Derived() {
System.out.println("Derived class constructor");
System.out.println("a = " + a_default);
//System.out.println("b = " + b_private);
System.out.println("c = " + c_protected);
System.out.println("d = " + d_public);
}

}

 

DifferentPackage.java

package a2;

import a1.Base;

public class DifferentPackage {

public DifferentPackage() {
System.out.println("Non-subclass different package constructor");
Base ob = new Base();
//System.out.println("ob.a = " + ob.a_default);
//System.out.println("ob.b = " + ob.b_private);
//System.out.println("ob.c = " + ob.c_protected);
System.out.println("ob.d = " + ob.d_public);
}

}

 

Main.java

package a2;

public class Main {

public static void main(String[] args) {
Derived2 ob1 = new Derived2();
DifferentPackage ob2 = new DifferentPackage();
}

}

 

We perform the following steps in order to compile the three programs from command prompt.

C:\>set classpath=C:\access

C:\>cd access

C:\access>cd a2

C:\access\a2>javac *.java

 

This is how we run the main program of a2 from command prompt.

C:\access\a2>cd ..

C:\access>java a2.Main
Base class constructor
a = 10
b = 20
c = 30
d = 40
Derived different package constructor
c = 30
d = 40
Non-subclass different package constructor
Base class constructor
a = 10
b = 20
c = 30
d = 40
ob.d = 40