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

Packages in JAVA

Packages in Java encapsulate groups of classes, interfaces, and sub-packages to prevent naming conflicts and improve organization. They provide controlled access for class members and are closely related to directory structures, with naming conventions typically following the reverse order of domain names. Java includes built-in packages as part of its API and allows users to create their own packages for custom classes.

Uploaded by

sufiyapraveen01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Packages in JAVA

Packages in Java encapsulate groups of classes, interfaces, and sub-packages to prevent naming conflicts and improve organization. They provide controlled access for class members and are closely related to directory structures, with naming conventions typically following the reverse order of domain names. Java includes built-in packages as part of its API and allows users to create their own packages for custom classes.

Uploaded by

sufiyapraveen01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

PACKAGES IN JAVA

Packages in Java are a mechanism that encapsulates a group of classes, sub-packages, and
interfaces. Packages are used for:

 Prevent naming conflicts by allowing classes with the same name to exist in different
packages, like college.staff.cse.Employee and college.staff.ee.Employee.

 They make it easier to organize, locate, and use classes, interfaces, and other components.

 Packages also provide controlled access for Protected members that are accessible within
the same package and by subclasses.

 Also, for default members (no access specifier) that are accessible only within the same
package.

Working of Java Packages

Directory Structure: Package names and directory structures are closely related. For example, if a
package name is college.staff.cse, then three directories are, college, staff, and cse, where cse is
inside staff, and staff is inside the college.

Naming Conventions: Package names are written in reverse order of domain names,
e.g., org.geeksforgeeks.practice. In a college, the convention might be:

 college.tech.cse

 college.tech.ee

 college.art.history

Example:
import java.util.*;

Accessing Classes Inside a Package

In Java, we can import classes from a package using either of the following methods:

1. Import a specific class:

import java.util.Vector;

This imports only the Vector class from the java.util package.

2. Import all classes from a package:

import java.util.*;
Types of Java Packages

 Built-in Packages

 User-defined Packages

1. Built-in Packages

These packages consist of many classes which are a part of Java API.Some of the commonly
used built-in packages are:

 java.lang: Contains language support classes(e.g classes which defines primitive data
types, math operations). This package is automatically imported.

 java.io: Contains classes for supporting input / output operations.

 java.util: Contains utility classes which implement data structures like Linked List,
Dictionary and support ; for Date / Time operations.

 java.applet: Contains classes for creating Applets.

 java.awt: Contain classes for implementing the components for graphical user interfaces
(like button,; menus etc). 6)

 java.net: Contain classes for supporting networking operations.

2. User-defined Packages

These are the packages that are defined by the user.


1. Create the Package:
First, we create a directory myPackage (name should be same as the name of the
package). Then create the MyClass inside the directory with the first statement being
the package names.
// Name of the package must be same as the directory
// under which this file is saved
package myPackage;

public class MyClass


{
public void getNames (String s)
{
System.out.println(s);
}
}

2. Use the Class in Program:


Now we will use the MyClass class in our program.
// import 'MyClass' class from 'names' myPackage
import myPackage.MyClass;

public class Geeks {


public static void main(String args[]) {

// Initializing the String variable


// with a value
String s = "GeeksforGeeks";

// Creating an instance of class MyClass in


// the package.
MyClass o = new MyClass();

o.getNames(s);
}
}

You might also like