SlideShare a Scribd company logo
1 of 11
Java Packages
Prepared by
V.Santhi
Assistant Professor
Department of Computer Application
Bon Secours College for Women
Thanjavur
Packages
O 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.
O The benefits of organising classes into packages
are:
O 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.
O Classes in packages can be hidden if we don’t
want other packages to access them.
O 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
O 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:
O Using fully qualified class name
java.lang.Math.sqrt(x);
O Import package and use class name directly
import java.lang.Math
Math.sqrt(x);
O Selected or all classes in packages can be imported:
O Implicit in all programs: import java.lang.*;
O 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();
}
}
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();
}
}

More Related Content

Similar to packages.ppt

Java - Packages Concepts
Java - Packages ConceptsJava - Packages Concepts
Java - Packages ConceptsVicter Paul
 
Packages access protection, importing packages
Packages   access protection, importing packagesPackages   access protection, importing packages
Packages access protection, importing packagesTharuniDiddekunta
 
Packages in java
Packages in javaPackages in java
Packages in javajamunaashok
 
packages in java & c++
packages in java & c++packages in java & c++
packages in java & c++pankaj chelak
 
THE PACKAGES CONCEPT IN JAVA PROGRAMMING.pptx
THE PACKAGES CONCEPT  IN JAVA PROGRAMMING.pptxTHE PACKAGES CONCEPT  IN JAVA PROGRAMMING.pptx
THE PACKAGES CONCEPT IN JAVA PROGRAMMING.pptxKavitha713564
 
Unit4 java
Unit4 javaUnit4 java
Unit4 javamrecedu
 
7.Packages and Interfaces(MB).ppt .
7.Packages and Interfaces(MB).ppt             .7.Packages and Interfaces(MB).ppt             .
7.Packages and Interfaces(MB).ppt .happycocoman
 
Packages,interfaces and exceptions
Packages,interfaces and exceptionsPackages,interfaces and exceptions
Packages,interfaces and exceptionsMavoori Soshmitha
 
Lecture-12 Java Packages and GUI Basics.ppt
Lecture-12 Java Packages and GUI Basics.pptLecture-12 Java Packages and GUI Basics.ppt
Lecture-12 Java Packages and GUI Basics.pptlirogal422
 
Class notes(week 7) on packages
Class notes(week 7) on packagesClass notes(week 7) on packages
Class notes(week 7) on packagesKuntal Bhowmick
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in javaVishnu Suresh
 

Similar to packages.ppt (20)

Java - Packages Concepts
Java - Packages ConceptsJava - Packages Concepts
Java - Packages Concepts
 
Java packages oop
Java packages oopJava packages oop
Java packages oop
 
Java packages
Java packagesJava packages
Java packages
 
Packages
PackagesPackages
Packages
 
Packages access protection, importing packages
Packages   access protection, importing packagesPackages   access protection, importing packages
Packages access protection, importing packages
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Packages
PackagesPackages
Packages
 
Practice Program-9-Packages-Unit 4.docx
Practice Program-9-Packages-Unit 4.docxPractice Program-9-Packages-Unit 4.docx
Practice Program-9-Packages-Unit 4.docx
 
packages in java & c++
packages in java & c++packages in java & c++
packages in java & c++
 
THE PACKAGES CONCEPT IN JAVA PROGRAMMING.pptx
THE PACKAGES CONCEPT  IN JAVA PROGRAMMING.pptxTHE PACKAGES CONCEPT  IN JAVA PROGRAMMING.pptx
THE PACKAGES CONCEPT IN JAVA PROGRAMMING.pptx
 
Unit4 java
Unit4 javaUnit4 java
Unit4 java
 
7.Packages and Interfaces(MB).ppt .
7.Packages and Interfaces(MB).ppt             .7.Packages and Interfaces(MB).ppt             .
7.Packages and Interfaces(MB).ppt .
 
Packages,interfaces and exceptions
Packages,interfaces and exceptionsPackages,interfaces and exceptions
Packages,interfaces and exceptions
 
Lecture-12 Java Packages and GUI Basics.ppt
Lecture-12 Java Packages and GUI Basics.pptLecture-12 Java Packages and GUI Basics.ppt
Lecture-12 Java Packages and GUI Basics.ppt
 
Class notes(week 7) on packages
Class notes(week 7) on packagesClass notes(week 7) on packages
Class notes(week 7) on packages
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in java
 
javapackage
javapackagejavapackage
javapackage
 
Java package
Java packageJava package
Java package
 

More from SanthiNivas

exception-handling-in-java.ppt
exception-handling-in-java.pptexception-handling-in-java.ppt
exception-handling-in-java.pptSanthiNivas
 
Introduction to PHP.ppt
Introduction to PHP.pptIntroduction to PHP.ppt
Introduction to PHP.pptSanthiNivas
 
static methods.pptx
static methods.pptxstatic methods.pptx
static methods.pptxSanthiNivas
 
transmission media.ppt
transmission media.ppttransmission media.ppt
transmission media.pptSanthiNivas
 
Internet Basics Presentation.pptx
Internet Basics Presentation.pptxInternet Basics Presentation.pptx
Internet Basics Presentation.pptxSanthiNivas
 
Features of Java.pptx
Features of Java.pptxFeatures of Java.pptx
Features of Java.pptxSanthiNivas
 
Output Devices.pptx
Output Devices.pptxOutput Devices.pptx
Output Devices.pptxSanthiNivas
 
Input Devices.pptx
Input Devices.pptxInput Devices.pptx
Input Devices.pptxSanthiNivas
 
Operating System File Management Unit v.pptx
Operating System File Management Unit v.pptxOperating System File Management Unit v.pptx
Operating System File Management Unit v.pptxSanthiNivas
 
Input and Output Devices
Input and Output DevicesInput and Output Devices
Input and Output DevicesSanthiNivas
 
DDA ALGORITHM.pdf
DDA ALGORITHM.pdfDDA ALGORITHM.pdf
DDA ALGORITHM.pdfSanthiNivas
 
Computer Graphics Unit 2
Computer Graphics Unit 2Computer Graphics Unit 2
Computer Graphics Unit 2SanthiNivas
 
Computer Graphics
Computer GraphicsComputer Graphics
Computer GraphicsSanthiNivas
 
Page Layout and Background
Page Layout and BackgroundPage Layout and Background
Page Layout and BackgroundSanthiNivas
 
3-D Transformation in Computer Graphics
3-D Transformation in Computer Graphics3-D Transformation in Computer Graphics
3-D Transformation in Computer GraphicsSanthiNivas
 
4. THREE DIMENSIONAL DISPLAY METHODS
4.	THREE DIMENSIONAL DISPLAY METHODS4.	THREE DIMENSIONAL DISPLAY METHODS
4. THREE DIMENSIONAL DISPLAY METHODSSanthiNivas
 
-Working with Text in Corel Draw
-Working with Text in Corel Draw-Working with Text in Corel Draw
-Working with Text in Corel DrawSanthiNivas
 

More from SanthiNivas (20)

exception-handling-in-java.ppt
exception-handling-in-java.pptexception-handling-in-java.ppt
exception-handling-in-java.ppt
 
Introduction to PHP.ppt
Introduction to PHP.pptIntroduction to PHP.ppt
Introduction to PHP.ppt
 
static methods.pptx
static methods.pptxstatic methods.pptx
static methods.pptx
 
Topologies.ppt
Topologies.pptTopologies.ppt
Topologies.ppt
 
transmission media.ppt
transmission media.ppttransmission media.ppt
transmission media.ppt
 
Internet Basics Presentation.pptx
Internet Basics Presentation.pptxInternet Basics Presentation.pptx
Internet Basics Presentation.pptx
 
Topologies.ppt
Topologies.pptTopologies.ppt
Topologies.ppt
 
Features of Java.pptx
Features of Java.pptxFeatures of Java.pptx
Features of Java.pptx
 
Output Devices.pptx
Output Devices.pptxOutput Devices.pptx
Output Devices.pptx
 
Input Devices.pptx
Input Devices.pptxInput Devices.pptx
Input Devices.pptx
 
Operating System File Management Unit v.pptx
Operating System File Management Unit v.pptxOperating System File Management Unit v.pptx
Operating System File Management Unit v.pptx
 
Input and Output Devices
Input and Output DevicesInput and Output Devices
Input and Output Devices
 
HTML
HTMLHTML
HTML
 
DDA ALGORITHM.pdf
DDA ALGORITHM.pdfDDA ALGORITHM.pdf
DDA ALGORITHM.pdf
 
Computer Graphics Unit 2
Computer Graphics Unit 2Computer Graphics Unit 2
Computer Graphics Unit 2
 
Computer Graphics
Computer GraphicsComputer Graphics
Computer Graphics
 
Page Layout and Background
Page Layout and BackgroundPage Layout and Background
Page Layout and Background
 
3-D Transformation in Computer Graphics
3-D Transformation in Computer Graphics3-D Transformation in Computer Graphics
3-D Transformation in Computer Graphics
 
4. THREE DIMENSIONAL DISPLAY METHODS
4.	THREE DIMENSIONAL DISPLAY METHODS4.	THREE DIMENSIONAL DISPLAY METHODS
4. THREE DIMENSIONAL DISPLAY METHODS
 
-Working with Text in Corel Draw
-Working with Text in Corel Draw-Working with Text in Corel Draw
-Working with Text in Corel Draw
 

Recently uploaded

Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxAnaBeatriceAblay2
 

Recently uploaded (20)

Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
 

packages.ppt

  • 1. Java Packages Prepared by V.Santhi Assistant Professor Department of Computer Application Bon Secours College for Women Thanjavur
  • 2. Packages O 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. O The benefits of organising classes into packages are: O 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. O Classes in packages can be hidden if we don’t want other packages to access them. O 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 O 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: O Using fully qualified class name java.lang.Math.sqrt(x); O Import package and use class name directly import java.lang.Math Math.sqrt(x); O Selected or all classes in packages can be imported: O Implicit in all programs: import java.lang.*; O 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. 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(); } }