Java packages and access modifiers

Packages

Packages are directories. Java classes are saved as .java file and are placed in directories.  The directories called packages. At the top of the Java classes there is a statement as:

package collections.quiz;

Java uses “.” to separate directories from sub directories. To access classes in other packages, Java uses the import statement as:

import java.util.ArrayList;

packages_access_modifier

 

What is the use of java packages?

Java packages are useful for the following reasons.

  1. Using packages helps you to organize your program. For example a program might have a database connection, networking and a GUI portion. You can assign each portion to its own package. Some of the packages could be reused in other programs.
  2. Packages help to avoid class name conflicts. Using packages allow you to use a specific name repeatedly if that is necessary.
  3. Packages allow programmers to control the access to the classes by using access modifiers.

 

Access Modifiers

The accessibility of the classes and members are managed through the following keywords: public, protected, package and private. However, the keyword package unlike the other keywords is not used. We don’t use package int i = 4; but int i = 4; without the package modifier.

  • public: A public class or member is accessible from any other classes.
  • protected: A protected class or member is accessible only from classes and members of the same package. Once you extend the class you can access the protected members even from other packages.
  • package: or default modifier is known by the absence of a keyword. A package class or member is accessible only from classes and members in the same package.
  • private: A private class or member is accessible only from the class and its members where it is defined.

 

Example 1: Introducing the access modifiers within the same package.

In the following example ClassA and ClassC are in the same package:

package ex01.package1;

ClassC can access the public, protected and the package variables, which are “w, x, y”.  ClassC has no access to the variable “z“, because it is private.

if this code is compiled and run, “123” is written to the standard output.

package ex01.package1;

public class ClassA{

	public int w = 1;
	protected int x = 2;
	int y = 3;
	private int z = 4;
}
--------------------------
package ex01.package1;

public class ClassC{

	public static void main(String[] args){

		ClassA ca = new ClassA();
		System.out.print(ca.w);
		System.out.print(ca.x);
		System.out.print(ca.y);
	}
}

 

Example 2: Demonstrating the access modifiers from different packages.

In the following example ClassA and ClassE are in two different packages namely:

1- package ex01.package1;

2- package ex01.package2;

To access ClassA, you should first import it from ClassE, because they are not in the same package as:

import ex01.package1.ClassA;

ClassE can access only the public variable, which is “w”.  ClassE has no access to the variables “x,y,z“.

if this code is compiled and run, “1” is written to the stander output.


package ex01.package1;

public class ClassA{

	public int w = 1;
	protected int x = 2;
	int y = 3;
	private int z = 4;
}
--------------------------
package ex01.package2;

import ex01.package1.ClassA;

public class ClassE{

	public static void main(String[] args){

		ClassA ca = new ClassA();
		System.out.print(ca.w);
		//System.out.print(ca.x);
		//System.out.print(ca.y);
		//System.out.print(ma.z);
	}
}

 

Example 3: How the access modifiers work by inheritance?

In the following example ClassA and ClassD are in two different packages and ClassD extends ClassA

1- package ex01.package1;

2- package ex01.package2;

To access ClassA, you should first import it from ClassD, because they are not in the same package see the statement below:

import ex01.package1.ClassA;

ClassD can access only the public and the protected variables, which are “w” and “x”.  ClassD has no access to the variables “y” and “z“.

if this code is compiled and run, “12” is written to the stander output.

package ex01.package1;

public class ClassA{

	public int w = 1;
	protected int x = 2;
	int y = 3;
	private int z = 4;
}
--------------------------
package ex01.package2;

import ex01.package1.ClassA;

public class ClassD extends ClassA{

	public static void main(String[] args){

		ClassD cd = new ClassD();
		System.out.print(cd.w);
		System.out.print(cd.x);
		//System.out.print(cd.y);
		//System.out.print(cd.z);
	}
}
Please, leave your questions, feedback and suggestions in the comments below!
Sarmaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.


Sar Maroof's new book
Java quizmaster for beginners (English)

Java quizmaster voor beginners (Dutch)
Sar Maroof is the author of two Java books namely, Java quizmaster for beginners and A guide to build a Java application.
Experience with developing web applications since 2001
Sun Certified JSP as well as EJB
CIW Certified Internet Webmaster
Applied Science in Physics HBO Amsterdam
As well as Bachellor Science in Physics.
 Subscribe To sarmaroof RSS Feeds! (Your Browser)
 Mail This Page To A Friend!
 Bookmark This Page!
Share on FacebookTweet about this on TwitterShare on RedditShare on LinkedIn

4 Comments

Add a Comment
  1. Very helpful, thanks for your effort!

    1. you are welcome!

  2. Great website! It looks very good! Maintain the helpful work!

  3. This shows real exrpitese. Thanks for the answer.

Leave a Reply

Your email address will not be published. Required fields are marked *

Sarmaroof © 2017