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 in java programming language ppt
PPT
packages.ppt
PPT
packages.ppt
PPT
Packages in java
PPT
Packages in java
PPTX
Packages in java
PPTX
javapackage,try,cthrow,finallytch,-160518085421 (1).pptx
PPT
packages.ppt
packages in java programming language ppt
packages.ppt
packages.ppt
Packages in java
Packages in java
Packages in java
javapackage,try,cthrow,finallytch,-160518085421 (1).pptx
packages.ppt

Similar to packages unit 5 .ppt (20)

PPTX
Java package
PDF
JAVA 2-studenttrreadexeceptionpackages.pdf
PPTX
packages in java object oriented programming
PPTX
java package in java.. in java packages.
PPTX
java package java package in java packages
PPT
Javapackages 4th semester
PPTX
Package.pptx
PPTX
Packages
PPTX
Lecture 11.pptx galgotias College of engineering and technology
PPTX
Packages in java
PPT
7.Packages and Interfaces(MB).ppt .
PDF
Packages access protection, importing packages
PPTX
Package in Java
PPT
9 cm604.26
PPTX
Packages,static,this keyword in java
PPTX
Understanding Java Packages in easy way |Java
PPT
PACKAGE.PPT12345678912345949745654646455
PPTX
Packages
PPTX
Java packages oop
Java package
JAVA 2-studenttrreadexeceptionpackages.pdf
packages in java object oriented programming
java package in java.. in java packages.
java package java package in java packages
Javapackages 4th semester
Package.pptx
Packages
Lecture 11.pptx galgotias College of engineering and technology
Packages in java
7.Packages and Interfaces(MB).ppt .
Packages access protection, importing packages
Package in Java
9 cm604.26
Packages,static,this keyword in java
Understanding Java Packages in easy way |Java
PACKAGE.PPT12345678912345949745654646455
Packages
Java packages oop
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
Lecture Notes Electrical Wiring System Components
PPTX
Sustainable Sites - Green Building Construction
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
DOCX
573137875-Attendance-Management-System-original
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PDF
Digital Logic Computer Design lecture notes
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
OOP with Java - Java Introduction (Basics)
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
PPT on Performance Review to get promotions
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
additive manufacturing of ss316l using mig welding
Lecture Notes Electrical Wiring System Components
Sustainable Sites - Green Building Construction
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
573137875-Attendance-Management-System-original
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Digital Logic Computer Design lecture notes
R24 SURVEYING LAB MANUAL for civil enggi
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Model Code of Practice - Construction Work - 21102022 .pdf
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
OOP with Java - Java Introduction (Basics)
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPT on Performance Review to get promotions
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Operating System & Kernel Study Guide-1 - converted.pdf
additive manufacturing of ss316l using mig welding

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