SlideShare a Scribd company logo
PACKAGES
Explained By:
Sarbjit Kaur.
Lecturer, Department of Computer Application,
PGG.C.G., Sector: 42, Chandigarh
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 can have the
same name. If there is a naming clash, then classes can be accessed
with their fully qualified name.
- Classes in packages can be hidden if we don’t want other packages
to access them.
- Packages also provide a way for separating “design” from coding.
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”.
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
• }
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.ppt (20)

PPTX
Package.pptx
VeenaNaik23
 
PPT
Packages in java
Jancypriya M
 
PPT
Packages in java
jamunaashok
 
PPTX
java package in java.. in java packages.
ArunPatrickK1
 
PPTX
java package java package in java packages
ArunPatrick2
 
PPTX
Java packages oop
Kawsar Hamid Sumon
 
PDF
JAVA 2-studenttrreadexeceptionpackages.pdf
msurfudeen6681
 
PPTX
Package in Java
lalithambiga kamaraj
 
PPTX
Packages,static,this keyword in java
Vishnu Suresh
 
PPT
Javapackages 4th semester
Varendra University Rajshahi-bangladesh
 
PPTX
Packages
Monika Mishra
 
PPTX
Packages in java
Elizabeth alexander
 
PPTX
Java packags
Padma Kannan
 
PPT
PACKAGE.PPT12345678912345949745654646455
meetjaju38
 
PPTX
Packages_lms power point presentation...
Betty333100
 
PDF
Packages access protection, importing packages
TharuniDiddekunta
 
PPTX
packages in java & c++
pankaj chelak
 
PPT
9 cm604.26
myrajendra
 
DOCX
Unit4 java
mrecedu
 
Package.pptx
VeenaNaik23
 
Packages in java
Jancypriya M
 
Packages in java
jamunaashok
 
java package in java.. in java packages.
ArunPatrickK1
 
java package java package in java packages
ArunPatrick2
 
Java packages oop
Kawsar Hamid Sumon
 
JAVA 2-studenttrreadexeceptionpackages.pdf
msurfudeen6681
 
Package in Java
lalithambiga kamaraj
 
Packages,static,this keyword in java
Vishnu Suresh
 
Javapackages 4th semester
Varendra University Rajshahi-bangladesh
 
Packages
Monika Mishra
 
Packages in java
Elizabeth alexander
 
Java packags
Padma Kannan
 
PACKAGE.PPT12345678912345949745654646455
meetjaju38
 
Packages_lms power point presentation...
Betty333100
 
Packages access protection, importing packages
TharuniDiddekunta
 
packages in java & c++
pankaj chelak
 
9 cm604.26
myrajendra
 
Unit4 java
mrecedu
 

Recently uploaded (20)

PDF
Portable Veterinary Ultrasound Scanners & Animal Medical Equipment - TcCryo
3447752272
 
PPTX
basic_parts-of_computer-1618-754-622.pptx
patelravi16187
 
PPTX
Basics of Memristors from zero to hero.pptx
onterusmail
 
PPTX
西班牙海牙认证瓦伦西亚国际大学毕业证与成绩单文凭复刻快速办理毕业证书
sw6vvn9s
 
PPTX
Normal distriutionvggggggggggggggggggg.pptx
JayeshTaneja4
 
PPT
community diagnosis slides show health. ppt
michaelbrucebwana
 
PDF
Endalamaw Kebede.pdfvvbhjjnhgggftygtttfgh
SirajudinAkmel1
 
PPTX
DOC-20250728-WAprocess releases large amounts of carbon dioxide (CO₂), sulfur...
samt56673
 
PPTX
Modern machinery.pptx sjsjnshhsnsnnjnnbbbb
raipureastha08
 
PPTX
Basics of Memristors and fundamentals.pptx
onterusmail
 
PPTX
PPT on the topic of programming language
dishasindhava
 
DOCX
What Is Zoning Map Software and Why It Matters for Communities
riffatparveenseo
 
PPTX
PPT FOR BASIC UNDERSTANDING OF COMPUTER HARDWARE, SOFTWARE & FIRMWARE
kavishvora10
 
PPTX
Save significantly on desk spaces and overheads with the KVM over IP software
AvexTender
 
PPT
Susunan & Bagian DRAWING 153UWYHSGDGH.ppt
RezaFbriadi
 
PPTX
原版UMiami毕业证文凭迈阿密大学学费单定制学历在线制作硕士毕业证
jicaaeb0
 
PDF
HUAWEI MOBILE PHONE IMPORTED FROM CHINA TO THAILAND REPORT.pdf.pdf
youyou851038
 
PPTX
Aryanbarot28.pptx Introduction of window os for the projects
aryanbarot004
 
PPTX
G6Q1 WEEK 2 SCIENCE PPT.pptxLVLLLLLLLLLLLLLLLLL
DitaSIdnay
 
PPTX
Boolean Algebra-Properties and Theorems.pptx
bhavanavarri5458
 
Portable Veterinary Ultrasound Scanners & Animal Medical Equipment - TcCryo
3447752272
 
basic_parts-of_computer-1618-754-622.pptx
patelravi16187
 
Basics of Memristors from zero to hero.pptx
onterusmail
 
西班牙海牙认证瓦伦西亚国际大学毕业证与成绩单文凭复刻快速办理毕业证书
sw6vvn9s
 
Normal distriutionvggggggggggggggggggg.pptx
JayeshTaneja4
 
community diagnosis slides show health. ppt
michaelbrucebwana
 
Endalamaw Kebede.pdfvvbhjjnhgggftygtttfgh
SirajudinAkmel1
 
DOC-20250728-WAprocess releases large amounts of carbon dioxide (CO₂), sulfur...
samt56673
 
Modern machinery.pptx sjsjnshhsnsnnjnnbbbb
raipureastha08
 
Basics of Memristors and fundamentals.pptx
onterusmail
 
PPT on the topic of programming language
dishasindhava
 
What Is Zoning Map Software and Why It Matters for Communities
riffatparveenseo
 
PPT FOR BASIC UNDERSTANDING OF COMPUTER HARDWARE, SOFTWARE & FIRMWARE
kavishvora10
 
Save significantly on desk spaces and overheads with the KVM over IP software
AvexTender
 
Susunan & Bagian DRAWING 153UWYHSGDGH.ppt
RezaFbriadi
 
原版UMiami毕业证文凭迈阿密大学学费单定制学历在线制作硕士毕业证
jicaaeb0
 
HUAWEI MOBILE PHONE IMPORTED FROM CHINA TO THAILAND REPORT.pdf.pdf
youyou851038
 
Aryanbarot28.pptx Introduction of window os for the projects
aryanbarot004
 
G6Q1 WEEK 2 SCIENCE PPT.pptxLVLLLLLLLLLLLLLLLLL
DitaSIdnay
 
Boolean Algebra-Properties and Theorems.pptx
bhavanavarri5458
 
Ad

packages.ppt

  • 1. PACKAGES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh
  • 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 can have the same name. If there is a naming clash, then classes can be accessed with their fully qualified name. - Classes in packages can be hidden if we don’t want other packages to access them. - Packages also provide a way for separating “design” from coding.
  • 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. 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.*;
  • 8. 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 • }
  • 9. 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(); } }
  • 10. 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.
  • 11. 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"); } }
  • 12. 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(); • } • }
  • 13. Output • Hello, I am ClassA • Hello, I am ClassC