SlideShare a Scribd company logo
PACKAGES
Packages
• Packages are Java’s way of grouping a number of
related classes and/or interfaces together into a single
unit. That means, packages act as “containers” for
classes.
• The benefits of organising classes into packages are:
- The classes contained in the packages of other
programs/applications can be reused.
- In packages classes can be unique compared with
classes in other packages. That two classes in two
different packages .
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 app.
Using System Packages
• The packages are organised in a hierarchical structure .For example, a
package named “java” contains the package “awt”, which in turn contains
various classes required for implementing GUI (graphical user interface).
Java “java” Package containing
“lang”, “awt”,.. packages;
Can also contain classes.
awt package containing classes.
Classes containing methods
lang
graphics
font
image
awt
Accessing Classes from Packages
There are two ways of accessing the classes stored in packages:
• Using fully qualified class name
java.lang.Math.sqrt(x);
• Import package and use class name directly
import java.lang.Math
Math.sqrt(x);
• Selected or all classes in packages can be imported:
• Implicit in all programs: import java.lang.*;
• package statement(s) must appear first
import package.class;
import package.*;
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 {
}
Package name is “myPackage” and classes are considered as part of
this package; The code is saved in a file called “ClassA.java” and located in
a directory called “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
• }
Using a Package
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
• en 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
Let us store the code listing below in a file named“ClassA.java” within
subdirectory named“secondPackage” within the current directory
(say“abc”).
public class ClassC {
// class body
public void display()
{
System.out.println("Hello, I am ClassC");
}
}
Using a Package
• 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
• Hello, I am ClassA
• Hello, I am ClassC
Thanks you

More Related Content

PPT
packages.ppt
PPT
packages.ppt
PPT
packages in java programming language ppt
PPT
packages.ppt
PPTX
Java packages
PPTX
Packages
PPTX
Packages in java
PPTX
java package in java.. in java packages.
packages.ppt
packages.ppt
packages in java programming language ppt
packages.ppt
Java packages
Packages
Packages in java
java package in java.. in java packages.

Similar to packages unit 5 .ppt (20)

PPTX
java package java package in java packages
DOCX
Unit4 java
PPTX
Java package
PPTX
Packages
PPTX
javapackage,try,cthrow,finallytch,-160518085421 (1).pptx
PDF
Class notes(week 7) on packages
PPT
9 cm604.26
PPT
Packages(9 cm604.26)
PDF
JAVA 2-studenttrreadexeceptionpackages.pdf
PPTX
Package in Java
PPTX
Packages in java
PDF
javapackage
PPTX
Java packages
PPTX
Package.pptx
PPT
7.Packages and Interfaces(MB).ppt .
DOCX
Practice Program-9-Packages-Unit 4.docx
PPTX
packages in java object oriented programming
DOCX
Class notes(week 7) on packages
PPTX
PPTX
Package In Java
java package java package in java packages
Unit4 java
Java package
Packages
javapackage,try,cthrow,finallytch,-160518085421 (1).pptx
Class notes(week 7) on packages
9 cm604.26
Packages(9 cm604.26)
JAVA 2-studenttrreadexeceptionpackages.pdf
Package in Java
Packages in java
javapackage
Java packages
Package.pptx
7.Packages and Interfaces(MB).ppt .
Practice Program-9-Packages-Unit 4.docx
packages in java object oriented programming
Class notes(week 7) on packages
Package In Java
Ad

More from thenmozhip8 (14)

PPTX
U5 SPC.pptx
PDF
Unit 4.pdf
PPTX
unit 3 ppt.pptx
PPT
U2.ppt
PPT
Unit 1 .ppt
DOCX
IR UNIT V.docx
PPTX
IRT Unit_4.pptx
DOCX
UNIT 3 IRT.docx
PPTX
IRT Unit_ 2.pptx
PPTX
IRT Unit_I.pptx
PPT
unit 4 .ppt
PPTX
Definning class.pptx unit 3
PPT
exception-handling-in-java.ppt unit 2
PPTX
unit 1 full ppt.pptx
U5 SPC.pptx
Unit 4.pdf
unit 3 ppt.pptx
U2.ppt
Unit 1 .ppt
IR UNIT V.docx
IRT Unit_4.pptx
UNIT 3 IRT.docx
IRT Unit_ 2.pptx
IRT Unit_I.pptx
unit 4 .ppt
Definning class.pptx unit 3
exception-handling-in-java.ppt unit 2
unit 1 full ppt.pptx
Ad

Recently uploaded (20)

PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
Internship_Presentation_Final engineering.pptx
PPT
Drone Technology Electronics components_1
PDF
BRKDCN-2613.pdf Cisco AI DC NVIDIA presentation
PPTX
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
PPTX
anatomy of limbus and anterior chamber .pptx
PDF
Geotechnical Engineering, Soil mechanics- Soil Testing.pdf
PDF
오픈소스 LLM, vLLM으로 Production까지 (Instruct.KR Summer Meetup, 2025)
PPTX
436813905-LNG-Process-Overview-Short.pptx
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
PDF
composite construction of structures.pdf
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
Road Safety tips for School Kids by a k maurya.pptx
PDF
Structs to JSON How Go Powers REST APIs.pdf
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
The-Looming-Shadow-How-AI-Poses-Dangers-to-Humanity.pptx
PPT
Project quality management in manufacturing
PPTX
ANIMAL INTERVENTION WARNING SYSTEM (4).pptx
PPTX
Practice Questions on recent development part 1.pptx
CH1 Production IntroductoryConcepts.pptx
Internship_Presentation_Final engineering.pptx
Drone Technology Electronics components_1
BRKDCN-2613.pdf Cisco AI DC NVIDIA presentation
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
anatomy of limbus and anterior chamber .pptx
Geotechnical Engineering, Soil mechanics- Soil Testing.pdf
오픈소스 LLM, vLLM으로 Production까지 (Instruct.KR Summer Meetup, 2025)
436813905-LNG-Process-Overview-Short.pptx
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
composite construction of structures.pdf
Operating System & Kernel Study Guide-1 - converted.pdf
Road Safety tips for School Kids by a k maurya.pptx
Structs to JSON How Go Powers REST APIs.pdf
bas. eng. economics group 4 presentation 1.pptx
The-Looming-Shadow-How-AI-Poses-Dangers-to-Humanity.pptx
Project quality management in manufacturing
ANIMAL INTERVENTION WARNING SYSTEM (4).pptx
Practice Questions on recent development part 1.pptx

packages unit 5 .ppt

  • 2. Packages • Packages are Java’s way of grouping a number of related classes and/or interfaces together into a single unit. That means, packages act as “containers” for classes. • The benefits of organising classes into packages are: - The classes contained in the packages of other programs/applications can be reused. - In packages classes can be unique compared with classes in other packages. That two classes in two different packages .
  • 3. 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 app.
  • 4. Using System Packages • The packages are organised in a hierarchical structure .For example, a package named “java” contains the package “awt”, which in turn contains various classes required for implementing GUI (graphical user interface). Java “java” Package containing “lang”, “awt”,.. packages; Can also contain classes. awt package containing classes. Classes containing methods lang graphics font image awt
  • 5. Accessing Classes from Packages There are two ways of accessing the classes stored in packages: • Using fully qualified class name java.lang.Math.sqrt(x); • Import package and use class name directly import java.lang.Math Math.sqrt(x); • Selected or all classes in packages can be imported: • Implicit in all programs: import java.lang.*; • package statement(s) must appear first import package.class; import package.*;
  • 6. 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 { } Package name is “myPackage” and classes are considered as part of this package; The code is saved in a file called “ClassA.java” and located in a directory called “myPackage”.
  • 7. 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 • }
  • 8. Using a Package 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(); } }
  • 9. Compiling and Running • en 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.
  • 10. Using a Package Let us store the code listing below in a file named“ClassA.java” within subdirectory named“secondPackage” within the current directory (say“abc”). public class ClassC { // class body public void display() { System.out.println("Hello, I am ClassC"); } }
  • 11. Using a Package • 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(); • } • }
  • 12. Output • Hello, I am ClassA • Hello, I am ClassC