You are on page 1of 25

Packages In

Java
MADE BY:
HARISH THAKUR(17BSC1071)
SAHIL KUMAR(17BSC1064)
AMIT RANA(17BSC1067)
Introduction
 The main feature of OOP is its ability to
support the reuse of code:
 Extending the classes (via inheritance)
 Extending interfaces
 The features in basic form limited to reusing
the classes within a program.
 What if we need to use classes from other
programs without physically copying them
into the program under development ?
 In Java, this is achieved by using what is
known as “packages”, a concept similar to
“class libraries” in other languages.
Packages
 Packages enable grouping of functionally related
classes
 Package names are dot separated, e.g.,
java.lang.
 Package names have a correspondence with the
directory structure
 Packages Avoid name space collision. There can
not be two classes with same name in a same
Package But two packages can have a class with
same name.
 Exact Name of the class is identified by its
package structure. << Fully Qualified Name>>
java.lang.String ; java.util.Arrays;
java.io.BufferedReader ; java.util.Date
Advantages of Java Package
 A Java package is mainly used for the
categorization of classes and interfaces so that we
can maintain them easily.
 They always provide access protection
 Used to bundle classes and interface.
 With the help of packages, we can reuse the
existing code
 By using package we can easily locate the classes
related to it.
 Also, remove the naming collision.
Java Foundation 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 applets
Creating Packages
 Java supports a keyword called “package” for
creating user-defined packages. The package
statement must be the first statement in a Java
source file (except comments and white spaces)
followed by one or more classes.
package
myPackage;
public class ClassA {
// class body
}
class ClassB {
// class body
}
 Package name is “myPackage” and classes
are considred as part of this package; The
code is saved in a file called “ClassA.java”
and located in a directory called
“myPackage”.
Creating Sub Packages
 Classes in one ore more source files can be
part of the same packages.
 Aspackages in Java are organised
hierarchically, sub-packages can be
created as follows:
 package myPackage.Math
 package myPackage.secondPakage.thirdPackage
 Store
“thirdPackage” in a subdirectory named
“myPackage\secondPackage”. Store
“secondPackage” and “Math” class in a
subdirectory “myPackage”.
Accessing a Package
 As indicated earlier, classes in
packages can be accessed using a
fully qualified name or using a short-cut
as long as we import a corresponding
package.
 The general form of importing
package is:
 import
package1[.package2][…].classname
 Example:
 import myPackage.ClassA;
 import myPackage.secondPackage
 All
classes/packages from higher-level
package can be imported as follows:
 import myPackage.*;
Using a Package
 Let
us store the code listing below in a file named
“ClassA.java” within subdirectory named
“myPackage” within the current directory (say
“abc”).
package myPackage;
public class ClassA {
// class body
public void display()
{
System.out.println("Hello, I am ClassA");
}
}
class ClassB {
// class body
}
 Within the current directory (“abc”) store
the following code in a file named
“ClassX.java”

import myPackage.ClassA;

public class ClassX


{
public static void main(String args[])
{
ClassA objA = new ClassA();
objA.display();
}
}
Compiling and Running
 When ClassX.java is compiled, the compiler
compiles it and places .class file in current
directly. If .class of ClassA in subdirectory
“myPackage” is not found, it comples ClassA
also.
 Note: It does not include code of ClassA into
ClassX
 When the program ClassX is run, java loader
looks for ClassA.class file in a package called
“myPackage” and loads it.
Using a Package
 Letus store the code listing below in a file
named “ClassA.java” within subdirectory
named “secondPackage” within the
current directory (say “abc”).
package secondPackage;
public class ClassC {
// class body
public void display()
{
System.out.println("Hello, I am ClassC");
}
}
 Within the current directory (“abc”) store
the following code in a file named
“ClassX.java”

import myPackage.ClassA;
import secondPackage.ClassC;
public class ClassY
{
public static void main(String args[])
{
ClassA objA = new ClassA();
ClassC objC = new ClassC();
objA.display();
objC.display();
}
}
Output

[raj@mundroo] package % java ClassY

Hello, I am ClassA
Hello, I am ClassC

[raj@mundroo] package %
Importing the Package
 import statement allows the importing of package
 Library packages are automatically imported
irrespective of the location of compiling and
executing program
 JRE looks at two places for user created packages
(i) Under the current working directory
(ii) At the location specified by CLASSPATH
environment variable
 Most ideal location for compiling/executing a
program is immediately above the package
structure.
 Example importing
import
mypackage.mypackageA.ABC;
import
mypackage.mypackageA.ABC.*;
class packagetest
{ << packagetest.java>>
public static void main(String args[])
{
B b1 = new B(); << Store it in location above the package
C c1 = new C(); structure. Compile and Execute it from
}
there>>
}

This is Class B
This is Class C
import mypackage.mypackageA.ABC.*;
Import mypackage.mypackageB.IJK.*;
class packagetest
{
public static void main(String args[]) << What’s Wrong Here>>
{
A a1 = new A();
}
}
mypackage.mypackageA.ABC.A a1 = new
mypackage.mypackageA.ABC.A();
OR
mypackage.mypackageB.IJK.A a1 = new
mypackage.mypackageB.IJK.A();
 << class A is present in both the imported
packages ABC and IJK. So A has to be
fully qualified in this case>>
Types of Packages in Java
Built-in Packages in Java
 Built-in is a part of Java API and it offers a variety of
packages are –
 lang – Automatically imported and it contains language
support classes.
 io – Contains classes for input and output operations.
 util – Contains utility classes for implementing data
structures.
 applet – This package contains classes that create
applets.
 awt – Contain classes that implement compounds for GUI.
 net – This package contains classes that support
networking operations.
User-defined Packages in
Java
 InJava, the user creates these packages
according to their requirements. For
example –
 We first create a directory named
myPackage and then a class named
myClass in it.
package com.dataflair.packageprogram;
public class MyClass
{
public void getNames(String name)
{
System.out.println(name);
}
}
package com.dataflair.packageprogramdemo;
import com.dataflair.packageprogram.MyClass;
public class MyClass1 {
public static void main(String args[])
{
// Initializing the String variable with a value
String name = "DataFlair";
// Creating an instance of class MyClass in the package.
MyClass obj = new MyClass();
obj.getNames(name);
}

You might also like