0% found this document useful (0 votes)
35 views16 pages

Access Control and JAVA Packages - 080204

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

Access Control and JAVA Packages - 080204

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

Programming in Java

JAVA Packages
Introduction
What is Package?
A package is a namespace that organizes a set of
related classes and interfaces.

Or
•A Java package is a mechanism for organizing Java
classes into namespaces.

•A package is a logical container of classes, interfaces


and sub- packages.
Package
• Package provides unique namespace for classes (i.e. facility
of uniquely identifying a class or interface and resolving name
conflicts).

• If a class or interface is stored in a package, then it is


referenced by using the package name.
Introduction
What is the need of Package?

•A unique name should be used for each class to


avoid name collisions.

•Without some way to manage the name space, we


could run out of convenient, descriptive names for
individual classes.
Reasons to use packages
• Grouping related classes.

• In large programs prevents name conflicts.

• Allows effective use of default "package" visibility.


Defining a Package
• Each file may have a package declaration which precedes
all non-comment code.

• The package name must be the same as the enclosing


directory.

• Default package:
If a package declaration is omitted, all classes in that
directory are said to belong to the "default" package.
Defining a Package
• package Package_Name;
Must be the first statement in a class or interface definition.

Example:
package P1;
public class Test
{
public static void main(String arr[])
{
System.out.println(“This is package P1”);
}
}
Compilation:
\Package \P1> javac Test.java

Execution:
\Package> java P1.Test

Note: To execute the class file of a package, we need to go


back to the parent folder of package and refer the class as-
package_name.class_name

•We can create the package while compiling the program.


Javac –d.Test.java
classes in different packages
package P1;
public class Test11
{
public void display()
{
System.out.println(“This is package P1”);
}
}
package P2;
import P1.Test11;
public class Test1
{
public static void main(String arr[])
{
P1.Test 11 t = new P1.Test11();
t.display(); }}
Importing a Package
• ‘import’ keyword is used to import the classes of one
package into another package.

import package_name.*;

• A class must be publically defined in the package in order to


be imported in other package.
• Once a class is imported in any package, it can be
referenced without package name.

• If a class is imported but not referenced in importing class,


then it will not be loaded (i.e. the imported class will be
loaded only when it is referenced ).
Summary About Package Concept in Java

* A package in Java is used to group related classes.


* Think of it as a folder in a file directory.
* We use packages to avoid name conflicts, and to write a better maintainable code.

Packages are divided into two categories:

* Built-in Packages (packages from the Java API)


* User-defined Packages (create your own packages)

Built-in Packages

The Java API is a library of pre written classes, that are free to use, included in the Java
Development Environment.

Note : The library contains components for managing input, database programming, and much
much more.

The complete list can be found at Oracles website:

https://docs.oracle.com/javase/8/docs/api/
To use a class or a package from the library, you need to use the import keyword:

import package.name.Class; // Import a single class


import package.name.*; // Import the whole package

Import a Class

If you find a class you want to use, for example, the Scanner class, which is used to get user
input.

Example : import java.util.Scanner;

In the example above, java.util is a package, while Scanner is a class of the java.util
package.

Note : To use the Scanner class, create an object of the class and use any of the available
methods found in the Scanner class documentation.

For example, we will use the nextLine() method, which is used to read a complete line.

User-defined Packages
To create your own package, you need to understand that Java uses a file system directory
to store them, just like folders on your computer.

Note : To create a package, use the package keyword.


Core Packages in Java
java.lang : basic language functionality and fundamental types
java.util : collection data structure classes
java.io : file operations
java.math : multiprecision arithmetics
java.nio : the New I/O framework for Java
java.net : networking operations, sockets
java.security : key generation, encryption and decryption
java.sql : Java Database Connectivity (JDBC) to access
java.awt databases
javax.swing : basic hierarchy of packages for native GUI
components
java.applet : hierarchy of packages for platform-independent rich
GUI components
: classes
Note: The java.lang for creating
package an applet
is available without the use of an import statement.
Access Specifiers
Access Specifiers
Specifier Sub class Non-Sub class Sub class Non-Sub class
(Same (Same Package) (Different (Different
Package) Package) Package)

Public Yes Yes Yes Yes

Protected Yes Yes Yes No

default Yes Yes No No

Private No No No No

You might also like