0% found this document useful (0 votes)
9 views5 pages

06 Package

The document provides an overview of packages in Java, defining them as namespaces that organize related classes and interfaces. It covers the types of packages, naming conventions, creation, and access methods, as well as the benefits of using packages for code organization and reuse. Additionally, it explains Java API packages and user-defined packages, along with static imports and access specifiers.

Uploaded by

alamk765432
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)
9 views5 pages

06 Package

The document provides an overview of packages in Java, defining them as namespaces that organize related classes and interfaces. It covers the types of packages, naming conventions, creation, and access methods, as well as the benefits of using packages for code organization and reuse. Additionally, it explains Java API packages and user-defined packages, along with static imports and access specifiers.

Uploaded by

alamk765432
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/ 5

JAVA PROGRAMMING

PACKAGE IN JAVA.

 DEFINE PACKAGE,
 TYPES OF PACKAGE
 NAMING CONVENTIONS
 CREATING PACKAGES,
 ACCESSING PACKAGE,
 IMPORT STATEMENT,
 STATIC IMPORT,
 ADDING CLASS & INTERFACES TO A PACKAGE

CONCEPT OF PACAKGE:
 You can think of packages as being similar to different folders on your computer.
o One folder might contains HTML pages.
o Second folder might contains images.
o Third folder might contains scripts or applications.
 And all these folders contents accessed without physical copy it in our java programme.
INTRODUCTION OF PACKAGE.
 A package is a namespace that organizes a set of related classes and interfaces.
 Software written in the Java programming language can be composed of hundreds or thousands of individual
classes, it makes sense to keep things organized by placing related classes and interfaces into packages.
Defination:
“Java provides a mechanism for partitioning the class namespace into more manageable parts. This mechanism is called
as “package‟. The package is both naming and visibility controlled mechanism”.
Some Points about Package.
 Packages are used for grouping a variety of classes & interfaces together.
 The grouping of package is done according to functionality.
 Packages acts as containers of classes.
 Packages are stored in hierarchical manner & are explicitly imported into new class definitions.

Benefits are achieved by organizing classes into packages:


1. The classes contained in the packages of other programs can be easily reused.
2. In packages, classes can be unique compared with classes in other packages. That is two classes in two different
packages can have same name. They may be referred by their fully qualified name, comprising package name &
class name.

Subject: Java Programming (Package) Print Date:10/Apr/2019 Page 1 of 5


3. Packages provide way to hide classes thus preventing other programs or packages from accessing classes that
are meant for internal use only.
4. Packages also provide a way for separating “design” from “coding”. First we can design classes & decide their
relationship & then we can implement Java code needed for methods. It is possible to change implementation of
any method without affecting rest of the design.

JAVA PACKAGES ARE CLASSIFIED INTO TWO TYPES.


 Java API packages
 User defined packages.
Java API Packages
These are the set of predefine classes stored in different packages according to functionality. Some of these packages
with their used are described below.
Package Name Description
. java.lang It contains language support classes.
These are classes that java compiler itself uses and therefore they
are automatically imported. They include classes for primitive types,
strings, math functions, threads and exceptions.
. java.util It contains language utility classes
such as vectors, hash tables, random numbers, date etc.
. java.io It contains input/output support classes.
They provide facilities for the input and output of data.

. java.awt It contains set of classes for implementing GUI.


They include classes for windows, buttons, lists, menus and so on.

. java.net It contains classes for networking.


They include classes for communicating with local computers as well
as with internet servers.
. java.applet It contains classes for creating and implementing applets.

PACKAGE NAMING CONVENTIONS.


 Java packages are genrally written in lowercase letters.
 Avoid creating packages with the same names as other public Java packages.
 It is recommended that you start your package hierarchy with the reverse domain name of your company.
For Example, since the domain name of my company is galaxy.edu then we should start with a package
structure called edu.galaxy.
In other words, a top level package named edu with a subpackage inside called galaxy.

Subject: Java Programming (Package) Print Date:10/Apr/2019 Page 2 of 5


CREATING PACKAGES

Before create the package,


 You need to create a source root / working directory on your hard disk.
 It is not itself part of the package structure.
 It contains all the Java sources that need to go, into your package structure.

Syntax:
package packagename; Here, packagename is the name of the package.

Steps to create a package are as follow.


 Create the new java source file.
 Include a package command as the first statement in a Java source file.
e.g package testpackage;
 Define the class that is to be put in the package testpackage with public access
public class TestClass
{
// data member declaration, if any
public void test_Method1()
{
// Access data member and other processing
}
void test_Method2()
{
// Access data member and other processing
}
}

o In this class, method test_Method1() is public so it is visible outside the class.


o Method test_Method2() is not public, so it is not visible outside the class but accessed internally from
test_Method1().
 Create the subfolder with the name of package testpackage in working directory e.g if your working folder is
D:\Galaxy.
 Store the new java source file ( TestClass.java ) in package folder e.g. testpackage.
o So directory structure is : D:\Galaxy \ testpackage \ TestClass.java
 Compile the file TestClass.java. This process creates TestClass.class file in subdirectory testpackage.
 Package packagename and class TestClass within it is created.

Additional Points To Remember For Knowledge.


 The package statement defines a name space in which classes are stored.
 If you omit the package statement, the class names are put into the default package, which has no name.

Subject: Java Programming (Package) Print Date:10/Apr/2019 Page 3 of 5


 Java is uses file system directories to store packages.
 The package subdirectory name must match with the package name exactly.
 You can define multiple classes in a java source file for internal use.
 Java supports the concept of package hierarchy.
o This is done by specifying multiple names in a package statement, separated by dots. E.g: package
firstPackage.secondPackage;
o To store this package in subdirectory named firstPackage/secondPackage.
 A java package file can have more than one class definition. in such cases only one of the classes may be
declared public & that class name with .java extension is the source file name. When a source file with more than
one class definition is complied, java creates independent .class files for these classes.

ACCESSING PACKAGES
Syntax: import pkg1[.pkg2].(classname.*);

 To access package In a create a new Java source file,


 Include import statements before class definitions to access expected package. E.g
Import testpackage;
 Define the class in which you want to access the classes of package testpackage.
public class AccessClass
{
}

 Save the AccessClass.java file in working directory i.e. outside the package folder.
D:\Galaxy \ AccessClass.java
 Create the object of class (TestClass), which is store in package folder and access it.
 Complete Defination of class is as follow
public class AccessClass
{
public static void main( String args[] )
{
TestClass A = new TestClass();
A. test_Method1(); // Accessible
A. test_Method2(); // Not Accessible because weaker access
}
}

Additional Points To Remember For Knowledge.


 When package contains more than one classes...
o To access any one of the class use : import packagename.SpecificClassName;
o To access all the classes use : import packagename.*;
 We can access a class from a package without import statement by giving its fully qualified class name
e.g. testpackage.TestClass A = new testpackage.TestClass();

Subject: Java Programming (Package) Print Date:10/Apr/2019 Page 4 of 5


 To access classes with more than one packages having same name, always use fully qualified class name.
e.g. testpackage.TestClass A = new testpackage.TestClass();
e.g. otherpackage.TestClass B = new otherpackage.TestClass();
Note : In the above example package testpackage and otherpackage are belongs from same working folder
and both having TestClass independantly.

Effect of access specifiers public, private and protected in package.


 Visibility restriction must be considered while using packages and inheritance.
 In program visibility restrictions are imposed by various access protection modifiers.
 Packages acts as container for classes and other package.
 Classes acts as container for data and methods.
 Data members and methods can be declared with the access protection modifiers such as private, protected and
public.
Following table shows the access protections

Static Import
 Static import is a feature which allows members (fields and methods) defined in a class as public static to be used
in Java code without specifying the class in which the field is defined.
 Example is as follow.
import static java.lang.System.out;
class TestABC
{
public static void main(String[] args)
{
out.println("Print Success …..");
}
}
 Observe that due to static import of System.out in this program, we can use println method without System class
name.

Subject: Java Programming (Package) Print Date:10/Apr/2019 Page 5 of 5

You might also like