0% found this document useful (0 votes)
22 views

Java Package

A package in Java is a mechanism for organizing classes and interfaces by grouping similar types together. There are two types of packages - built-in packages that are predefined in the Java API, and user-defined packages that are created as needed. Packages provide benefits like maintenance, reusability by preventing naming conflicts, and access protection. To use classes from another package, they can be imported either specifically by class name or generally using a wildcard, or accessed directly using the fully qualified name.

Uploaded by

kd ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Java Package

A package in Java is a mechanism for organizing classes and interfaces by grouping similar types together. There are two types of packages - built-in packages that are predefined in the Java API, and user-defined packages that are created as needed. Packages provide benefits like maintenance, reusability by preventing naming conflicts, and access protection. To use classes from another package, they can be imported either specifically by class name or generally using a wildcard, or accessed directly using the fully qualified name.

Uploaded by

kd ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Package: A package is a mechanism to group the similar type of classes, interfaces and sub-

packages that provide access control, protection and namespace management. It organizes classes
into single unit.
In Java already many predefined packages are available, used while programming.
For example: java.lang, java.io, java.util etc.
Program Example:
package P1;
public class Class1
{
}
public class Class2
{
}

Types of Packages:
There are two types of packages available in Java.
 Built-in packages: Built-in packages are already defined in java API. For example: java.util,
java.io, java.lang, java.awt, java.applet, java.net, etc.
 User defined packages: The package we create according to our need is called user defined
package.

Advantage of using packages in Java:


i. Maintenance: Java packages are used for proper maintenance. If any developer newly
joined a company, he can easily reach to files needed.
ii. Re-usability: The classes contained in the packages of another program can be easily reused.
iii. Name Conflicts: Packages help to resolve the naming conflict between the two classes with
the same name. For example, there can be two classes with the name Student in two
packages, university.csdept.Student and college.itdept.Student
iv. Organized: It also helps in organizing the files within our project.
v. Data Encapsulation: They provide a way to hide classes, preventing other programs from
accessing classes that are meant for internal use only.
vi. Access Protection: A package provides access protection. It can be used to provide visibility
control. The members of the class can be defined in such a manner that they will be visible
only to elements of that package.
How to access package from another package?
There are three ways to access the package from outside the package.
i. Import package. *;
ii. Import package.classname;
iii. fully qualified name.
01.Using packagename.*
 If we use package.* then all the classes and interfaces of this package will be accessible but not
subpackages.
 The import keyword is used to make the classes and interface of another package accessible to
the current package.
Example of package that import the packagename.*

//save by A.java
package pack;
public class A{
public void msg(){System.out.println("Hello");}
}

//save by B.java
package mypack;
import pack.*;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
OUTPUT:
Hello

02.Using packagename.classname
 If we import package.classname then only declared class of this package will be accessible.

Example of package by import package.classname

//save by A.java
package pack;
public class A{
public void msg()(System.out.println("Hello");)
}

//save by B.java
package import pack.A;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg():
}
}
Output:
Hello
3. Using fully qualified name

 If you use fully qualified name then only declared class of this package will be accessible.
Now there is no need to import. But you need to use fully qualified name every time when
you are accessing the class or interface.
 It is generally used when two packages have same class name e.g. java.util and java.sql
packages contain Date class.

Example of package by import fully qualified name

//save by A. java
package pack;
public class A{
public void msg()(System.out.println("Hello");)

//save by B.java
package mypack;
class B{
public static void main(String args[]){
pack.A obj = new pack.A()://using fully qualifiedname
obj.msg0);
}
}
Output:
Hello

You might also like