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

Similar to packages unit 5 .ppt (20)

PPTX
java package java package in java packages
ArunPatrick2
 
DOCX
Unit4 java
mrecedu
 
PPTX
Java package
CS_GDRCST
 
PPTX
Packages
Monika Mishra
 
PPTX
javapackage,try,cthrow,finallytch,-160518085421 (1).pptx
ArunPatrick2
 
PDF
Class notes(week 7) on packages
Kuntal Bhowmick
 
PPT
9 cm604.26
myrajendra
 
PPT
Packages(9 cm604.26)
myrajendra
 
PDF
JAVA 2-studenttrreadexeceptionpackages.pdf
msurfudeen6681
 
PPTX
Package in Java
lalithambiga kamaraj
 
PPTX
Packages in java
Elizabeth alexander
 
PDF
javapackage
Arjun Shanka
 
PPTX
Java packages
Shreyans Pathak
 
PPTX
Package.pptx
VeenaNaik23
 
PPT
7.Packages and Interfaces(MB).ppt .
happycocoman
 
DOCX
Practice Program-9-Packages-Unit 4.docx
R.K.College of engg & Tech
 
DOCX
Class notes(week 7) on packages
Kuntal Bhowmick
 
PPTX
Packages
MSharmilaDeviITDEPT
 
PPTX
Package In Java
maulik vadher
 
PPT
PACKAGE.PPT12345678912345949745654646455
meetjaju38
 
java package java package in java packages
ArunPatrick2
 
Unit4 java
mrecedu
 
Java package
CS_GDRCST
 
Packages
Monika Mishra
 
javapackage,try,cthrow,finallytch,-160518085421 (1).pptx
ArunPatrick2
 
Class notes(week 7) on packages
Kuntal Bhowmick
 
9 cm604.26
myrajendra
 
Packages(9 cm604.26)
myrajendra
 
JAVA 2-studenttrreadexeceptionpackages.pdf
msurfudeen6681
 
Package in Java
lalithambiga kamaraj
 
Packages in java
Elizabeth alexander
 
javapackage
Arjun Shanka
 
Java packages
Shreyans Pathak
 
Package.pptx
VeenaNaik23
 
7.Packages and Interfaces(MB).ppt .
happycocoman
 
Practice Program-9-Packages-Unit 4.docx
R.K.College of engg & Tech
 
Class notes(week 7) on packages
Kuntal Bhowmick
 
Package In Java
maulik vadher
 
PACKAGE.PPT12345678912345949745654646455
meetjaju38
 

More from thenmozhip8 (14)

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

Recently uploaded (20)

PPTX
Types of Bearing_Specifications_PPT.pptx
PranjulAgrahariAkash
 
PPTX
Evaluation and thermal analysis of shell and tube heat exchanger as per requi...
shahveer210504
 
PPTX
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
PPTX
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
PDF
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
PDF
Unified_Cloud_Comm_Presentation anil singh ppt
anilsingh298751
 
PDF
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
PPT
PPT2_Metal formingMECHANICALENGINEEIRNG .ppt
Praveen Kumar
 
PPTX
Green Building & Energy Conservation ppt
Sagar Sarangi
 
PDF
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
DOCX
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
PPTX
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
PPTX
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
PDF
Design Thinking basics for Engineers.pdf
CMR University
 
PPTX
Hashing Introduction , hash functions and techniques
sailajam21
 
PPTX
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
PPTX
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
PDF
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
DOC
MRRS Strength and Durability of Concrete
CivilMythili
 
PDF
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
Types of Bearing_Specifications_PPT.pptx
PranjulAgrahariAkash
 
Evaluation and thermal analysis of shell and tube heat exchanger as per requi...
shahveer210504
 
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
Unified_Cloud_Comm_Presentation anil singh ppt
anilsingh298751
 
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
PPT2_Metal formingMECHANICALENGINEEIRNG .ppt
Praveen Kumar
 
Green Building & Energy Conservation ppt
Sagar Sarangi
 
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
Design Thinking basics for Engineers.pdf
CMR University
 
Hashing Introduction , hash functions and techniques
sailajam21
 
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
MRRS Strength and Durability of Concrete
CivilMythili
 
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
Ad

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