Packages in Java
Packages in Java
interfaces together into a single unit. That means, packages act as “containers”
for classes.
• The benefits of organising classes into packages are:
- The classes contained in the packages of other programs/applications can be
reused.
- In packages classes can be unique compared with classes in other packages. ie
two classes in two different packages can have the same name. If there is a
naming clash, then classes can be accessed with their fully qualified name.
- Classes in packages can be hidden if we don’t want other packages to access
them.
- Packages also provide a way for separating “design” from coding.
JAVA API PACKAGES
Java provides a large number of classes groped into different packages based on
their functionality
• The six foundation Java packages are:
- java.lang :
Contains classes for primitive types, strings, math functions, threads, and exception.
- java.util :
Contains classes such as vectors, hash tables, date etc.
- java.io :
Stream classes for I/O .
- java.awt :
Classes for implementing GUI – windows, buttons, menus etc.
- java.net :
Classes for networking .
- java.applet :
Classes for creating and implementing app.
Using System Packages
awt
awt package containing classes.
graphics
font
Classes containing methods
image
Accessing Classes from
Packages
There are two ways of accessing the classes stored in packages:
• Using fully qualified class name
java.lang.Math.sqrt(x);
• Import package and use class name directly
import java.lang.Math
Math.sqrt(x);
• Selected or all classes in packages can be imported:
import package.class;
import package.*;
04/21/2025 SOBUZ 16
Adding a Class to a Package
04/21/2025 18
Creating Packages
04/21/2025 19
Example2-Package
package p2; import p1.ClassA;
public class ClassB import p2.ClassB;
{
protected int m =10; class PackageTest2
{
public void displayB()
public static void main(String str[])
{
{
System.out.println(“Class B”);
ClassA obA=new ClassA();
System.out.println(“m= “+m);
Classb obB=new ClassB();
}
obA.displayA();
} obB.displayB();
}
}
04/21/2025 20
Example 3- Package
import p2.ClassB; class PackageTest3
class ClassC extends ClassB {
{ public static void main(String args[])
int n=20; {
void displayC() ClassC obC = new ClassC();
{ obC.displayB();
System.out.println(“Class C”); obC.displayC();
System.out.println(“m= “+m); }
System.out.println(“n= “+n); }
}
}
04/21/2025 21
Package
package p1; Correct Code:
public class Teacher
{………….}
import p1.*;
public class Student import p2.*;
{……………..}
In Different files
p1.Student student1;
package p2;
public class Courses p2.Student student2;
{………..}
public class Student
{………………..}..Different files
import p1.*;
import p2.*;
Student student1; //Error
04/21/2025 22
Default Package
If a source file does not begin with the package statement, the classes
contained in the source file reside in the default package
The java compiler automatically looks in the default package to find
classes.
04/21/2025 23
Finding Packages
Two ways:
1.By default, java runtime system uses current directory as starting point and
search all the subdirectories for the package.
2.Specify a directory path using CLASSPATH environmental variable.
04/21/2025 24
CLASSPATH Environment Variable
The compiler and runtime interpreter know how to find standard
packages such as java.lang and java.util
The CLASSPATH environment variable is used to direct the compiler
and interpreter to where programmer defined imported packages can
be found
The CLASSPATH environment variable is an ordered list of
directories and files
04/21/2025 25
CLASSPATH Environment Variable
04/21/2025 31
public/package/private scope
Scope is concerned with the visibility of program
elements such as classes and members
Class members (methods or instance fields) can be
defined with public, package (default), private or
protected scope
A class has two levels of visibility:
-public scope means it is visible outside its containing
package
- default scope means it is visible only inside the
package. (package scope/ friendly scope)
04/21/2025 32
public/package/private scope
A class member with public scope means it is visible
anywhere its class is visible
A class member with private scope means it is visible only
within its encapsulating class
A class/class member with package scope means it is
visible only inside its containing package
A class member with protected scope means it is visible
every where except the non-subclasses in other package.
04/21/2025 33
Example 1
package my_package;
04/21/2025 34
Example-2
package my_package;
class D
{
// D’s public & private members
04/21/2025 35
Example-3
package another_package;
import my_package.*;
class C
{
// C’s public & private members
04/21/2025 36
Example 4
package my_package;
class A
{
int get() { return data; } // package
scope
public A(int d) { data=d;} // public
scope
private int data;
// private scope
}
class B
{
void f()
{
A a=new A(d); // OK A has package scope
int d=a.get(); // OK – get() has package
scope
04/21/2025
int d1=a.data; SOBUZ // Error! – data is private 37
Levels of Access Control
Np\no
04/21/2025 38
Access Protection in Java
Packages
• You might be aware of various aspects of Java’s access control
mechanism and its access specifiers. Packages in Java add another
dimension to access control. Both classes and packages are a
means of data encapsulation. While packages act as containers for
classes and other subordinate packages, classes act as containers
for data and code. Because of this interplay between packages and
classes, Java packages addresses four categories of visibility for
class members:
• Sub-classes in the same package
• Non-subclasses in the same package
• Sub-classes in different packages
• Classes that are neither in the same package nor sub-classes
No Modifier Protected Public
Private
Same Package
No Yes Yes Yes
Subclasses
Different Packages
No No Yes Yes
Subclasses