SlideShare a Scribd company logo
PACKAGES IN JAVA
Elizabeth Alexander
Hindustan University
PACKAGES
● Packages in Java is a mechanism to encapsulate a group of classes, interfaces
and sub packages.
● In java there are already many predefined packages that we use while
programming.
■ For example: java.lang, java.io, java.util etc.
● One of the most useful feature of java is that we can define our own packages
PACKAGES Cont...
Advantages of using a package
● Reusability: Reusability of code is one of the most important requirements in the
software industry. Reusability saves time, effort and also ensures consistency. A
class once developed can be reused by any number of programs wishing to
incorporate the class in that particular program.
● Easy to locate the files.
● In real life situation there may arise scenarios where we need to define files of the
same name. This may lead to “name-space collisions”. Packages are a way of
avoiding “name-space collisions”.
PACKAGES Cont...
● Many implementations of Java use a hierarchical file system to manage source
and class files. It is easy to organize class files into packages. All we need to do is
put related class files in the same directory, give the directory a name that relates
to the purpose of the classes, and add a line to the top of each class file that
declares the package name, which is the same as the directory name where they
reside.
● Types of package:
■ 1) User defined package: The package we create is called user-defined
package.
■ 2) Built-in package: The already defined package like java.io.*,
java.lang.* etc are known as built-in packages.
Packages in java
PACKAGES Cont...
Defining a Package:
● This statement should be used in the beginning of the program to include that
program in that particular package.
package <package name>;
● The package keyword is used to create a package in java.
To Compile: javac -d directory javafilename.java
To Run: java packagename.classname
● The -d switch specifies the destination where to put the generated class file
● If you want to keep the package within the same directory, you can use . (dot).
How to access package from another package?
There are three ways to access the package from outside the package.
1. import package.*;
2. import package.classname;
3. fully qualified name.
Using packagename.*
● If you use package.* then all the classes and interfaces of this package will be
accessible but not subpackages.
● The import keyword is used to make the classes and interface of another package
accessible to the current package.
Using packagename.classname
● If you import package.classname then only declared class of this package will be
accessible.
Using fully qualified name
● If you use fully qualified name then only declared class of this package will be
accessible.
● Now there is no need to import. But you need to use fully qualified name every
time when you are accessing the class or interface.
● It is generally used when two packages have same class name e.g. java.util and
java.sql packages contain Date class.
Note: If you import a package, all the classes and interface of that package will be
imported excluding the classes and interfaces of the subpackages. Hence, you need to
import the subpackage as well.
Sequence of the program must be package then import then class.
Packaging up multiple classes
Package with Multiple Public Classes
● A Java source file can have only one class declared as public, we cannot put two
or more public classes together in a.java file. This is because of the restriction
that the file name should be same as the name of the public class with
.javaextension.
● If we want to multiple classes under consideration are to be declared as public,
we have to store them in separate source files and attach the package
statement as the first statement in those source files.
//Save as A.java //Save as B.java
package javapoint; package java
Public class B{} public class a{}
CLASSPATH
● It is a environmental variable, which contains the path for the default-working
directory (.).
● The specific location that java compiler will consider, as the root of any package
hierarchy is, controlled by Classpath
● The package name is closely associated with the directory structure used to store
the classes. The classes (and other entities) belonging to a specific package are
stored together in the same directory.
● There is a scenario, I want to put the class file of A.java source file in classes
folder of c: drive. For example:
//save as Simple.java
package mypack;
public class Simple{
public static void main(String args[]){
System.out.println("Welcome to package");
}
}
To Compile:
e:sources> javac -d c:classes Simple.java
To Run:
To run this program from e:source directory, you need to set classpath of the directory
where the class file resides.
e:sources> set classpath=c:classes;
e:sources> java mypack.Simple
Another way to run this program by -classpath switch of
java:
● The -classpath switch can be used with javac and java tool.
● To run this program from e:source directory, you can use -classpath switch of
java that tells where to look for class file. For example:
■ e:sources> java -classpath c:classes mypack.Simple
Access Protection in Packages
● Access modifiers define the scope of the class and its members (data and
methods).
● There are four categories, provided by Java regarding the visibility of the class
members between classes and packages:
■ Subclasses in the same package
■ Non-subclasses in the same package
■ Subclasses in different packages
■ Classes that are neither in the same package nor subclasses
Packages in java
Ad

More Related Content

What's hot (20)

java interface and packages
java interface and packagesjava interface and packages
java interface and packages
VINOTH R
 
Java packages
Java packagesJava packages
Java packages
BHUVIJAYAVELU
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
BHUVIJAYAVELU
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
VINOTH R
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
Shubham Dwivedi
 
I/O Streams
I/O StreamsI/O Streams
I/O Streams
Ravi Chythanya
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
Pavith Gunasekara
 
Packages in java
Packages in javaPackages in java
Packages in java
Kavitha713564
 
Java thread life cycle
Java thread life cycleJava thread life cycle
Java thread life cycle
Archana Gopinath
 
Wrapper class
Wrapper classWrapper class
Wrapper class
kamal kotecha
 
Type casting in java
Type casting in javaType casting in java
Type casting in java
Farooq Baloch
 
Operators in java
Operators in javaOperators in java
Operators in java
AbhishekMondal42
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
Madishetty Prathibha
 
This keyword in java
This keyword in javaThis keyword in java
This keyword in java
Hitesh Kumar
 
Interface
InterfaceInterface
Interface
kamal kotecha
 
Event handling
Event handlingEvent handling
Event handling
swapnac12
 
Applets in java
Applets in javaApplets in java
Applets in java
Wani Zahoor
 
Control structures in java
Control structures in javaControl structures in java
Control structures in java
VINOTH R
 
Operators in java
Operators in javaOperators in java
Operators in java
Then Murugeshwari
 

Similar to Packages in java (20)

Packages in java
Packages in javaPackages in java
Packages in java
SahithiReddyEtikala
 
javapackage
javapackagejavapackage
javapackage
Arjun Shanka
 
JAVA 2-studenttrreadexeceptionpackages.pdf
JAVA 2-studenttrreadexeceptionpackages.pdfJAVA 2-studenttrreadexeceptionpackages.pdf
JAVA 2-studenttrreadexeceptionpackages.pdf
msurfudeen6681
 
java package java package in java packages
java package java package in java packagesjava package java package in java packages
java package java package in java packages
ArunPatrick2
 
java package in java.. in java packages.
java package in java.. in java packages.java package in java.. in java packages.
java package in java.. in java packages.
ArunPatrickK1
 
javapackage,try,cthrow,finallytch,-160518085421 (1).pptx
javapackage,try,cthrow,finallytch,-160518085421 (1).pptxjavapackage,try,cthrow,finallytch,-160518085421 (1).pptx
javapackage,try,cthrow,finallytch,-160518085421 (1).pptx
ArunPatrick2
 
PACKAGE.PPT12345678912345949745654646455
PACKAGE.PPT12345678912345949745654646455PACKAGE.PPT12345678912345949745654646455
PACKAGE.PPT12345678912345949745654646455
meetjaju38
 
Java packages oop
Java packages oopJava packages oop
Java packages oop
Kawsar Hamid Sumon
 
Packages
PackagesPackages
Packages
Monika Mishra
 
packages.ppt
packages.pptpackages.ppt
packages.ppt
SanthiNivas
 
Package in Java
Package in JavaPackage in Java
Package in Java
lalithambiga kamaraj
 
Java packages
Java packagesJava packages
Java packages
Shreyans Pathak
 
Packages
PackagesPackages
Packages
Nuha Noor
 
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
Kavitha713564
 
Java - Packages Concepts
Java - Packages ConceptsJava - Packages Concepts
Java - Packages Concepts
Victer Paul
 
Class notes(week 7) on packages
Class notes(week 7) on packagesClass notes(week 7) on packages
Class notes(week 7) on packages
Kuntal Bhowmick
 
Packages in java
Packages in javaPackages in java
Packages in java
jamunaashok
 
Packages in java
Packages in javaPackages in java
Packages in java
Jancypriya M
 
Packages
PackagesPackages
Packages
MSharmilaDeviITDEPT
 
Javapackages 4th semester
Javapackages 4th semesterJavapackages 4th semester
Javapackages 4th semester
Varendra University Rajshahi-bangladesh
 
JAVA 2-studenttrreadexeceptionpackages.pdf
JAVA 2-studenttrreadexeceptionpackages.pdfJAVA 2-studenttrreadexeceptionpackages.pdf
JAVA 2-studenttrreadexeceptionpackages.pdf
msurfudeen6681
 
java package java package in java packages
java package java package in java packagesjava package java package in java packages
java package java package in java packages
ArunPatrick2
 
java package in java.. in java packages.
java package in java.. in java packages.java package in java.. in java packages.
java package in java.. in java packages.
ArunPatrickK1
 
javapackage,try,cthrow,finallytch,-160518085421 (1).pptx
javapackage,try,cthrow,finallytch,-160518085421 (1).pptxjavapackage,try,cthrow,finallytch,-160518085421 (1).pptx
javapackage,try,cthrow,finallytch,-160518085421 (1).pptx
ArunPatrick2
 
PACKAGE.PPT12345678912345949745654646455
PACKAGE.PPT12345678912345949745654646455PACKAGE.PPT12345678912345949745654646455
PACKAGE.PPT12345678912345949745654646455
meetjaju38
 
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
Kavitha713564
 
Java - Packages Concepts
Java - Packages ConceptsJava - Packages Concepts
Java - Packages Concepts
Victer Paul
 
Class notes(week 7) on packages
Class notes(week 7) on packagesClass notes(week 7) on packages
Class notes(week 7) on packages
Kuntal Bhowmick
 
Packages in java
Packages in javaPackages in java
Packages in java
jamunaashok
 
Ad

More from Elizabeth alexander (11)

Java awt (abstract window toolkit)
Java awt (abstract window toolkit)Java awt (abstract window toolkit)
Java awt (abstract window toolkit)
Elizabeth alexander
 
Java applet
Java appletJava applet
Java applet
Elizabeth alexander
 
Io streams
Io streamsIo streams
Io streams
Elizabeth alexander
 
Multithreading programming in java
Multithreading programming in javaMultithreading programming in java
Multithreading programming in java
Elizabeth alexander
 
Exception handling in java
Exception handling  in javaException handling  in java
Exception handling in java
Elizabeth alexander
 
Java interfaces
Java   interfacesJava   interfaces
Java interfaces
Elizabeth alexander
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
Elizabeth alexander
 
Inheritance in Java
Inheritance in JavaInheritance in Java
Inheritance in Java
Elizabeth alexander
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
Elizabeth alexander
 
Quantitative Aptitude- Number System
Quantitative Aptitude- Number SystemQuantitative Aptitude- Number System
Quantitative Aptitude- Number System
Elizabeth alexander
 
Java Programming
Java ProgrammingJava Programming
Java Programming
Elizabeth alexander
 
Ad

Recently uploaded (20)

Mathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdfMathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdf
TalhaShahid49
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
Machine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptxMachine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptx
rajeswari89780
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
inmishra17121973
 
Raish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdfRaish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdf
RaishKhanji
 
The Gaussian Process Modeling Module in UQLab
The Gaussian Process Modeling Module in UQLabThe Gaussian Process Modeling Module in UQLab
The Gaussian Process Modeling Module in UQLab
Journal of Soft Computing in Civil Engineering
 
Compiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptxCompiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptx
RushaliDeshmukh2
 
Metal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistryMetal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistry
mee23nu
 
Introduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptxIntroduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptx
AS1920
 
Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
IntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdfIntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdf
Luiz Carneiro
 
ELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdfELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdf
Shiju Jacob
 
Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E..."Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
Infopitaara
 
Avnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights FlyerAvnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights Flyer
WillDavies22
 
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdfMAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
ssuser562df4
 
DSP and MV the Color image processing.ppt
DSP and MV the  Color image processing.pptDSP and MV the  Color image processing.ppt
DSP and MV the Color image processing.ppt
HafizAhamed8
 
theory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptxtheory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptx
sanchezvanessa7896
 
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
charlesdick1345
 
Mathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdfMathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdf
TalhaShahid49
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
Machine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptxMachine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptx
rajeswari89780
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
inmishra17121973
 
Raish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdfRaish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdf
RaishKhanji
 
Compiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptxCompiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptx
RushaliDeshmukh2
 
Metal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistryMetal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistry
mee23nu
 
Introduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptxIntroduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptx
AS1920
 
Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
IntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdfIntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdf
Luiz Carneiro
 
ELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdfELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdf
Shiju Jacob
 
Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E..."Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
Infopitaara
 
Avnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights FlyerAvnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights Flyer
WillDavies22
 
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdfMAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
ssuser562df4
 
DSP and MV the Color image processing.ppt
DSP and MV the  Color image processing.pptDSP and MV the  Color image processing.ppt
DSP and MV the Color image processing.ppt
HafizAhamed8
 
theory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptxtheory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptx
sanchezvanessa7896
 
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
charlesdick1345
 

Packages in java

  • 1. PACKAGES IN JAVA Elizabeth Alexander Hindustan University
  • 2. PACKAGES ● Packages in Java is a mechanism to encapsulate a group of classes, interfaces and sub packages. ● In java there are already many predefined packages that we use while programming. ■ For example: java.lang, java.io, java.util etc. ● One of the most useful feature of java is that we can define our own packages
  • 3. PACKAGES Cont... Advantages of using a package ● Reusability: Reusability of code is one of the most important requirements in the software industry. Reusability saves time, effort and also ensures consistency. A class once developed can be reused by any number of programs wishing to incorporate the class in that particular program. ● Easy to locate the files. ● In real life situation there may arise scenarios where we need to define files of the same name. This may lead to “name-space collisions”. Packages are a way of avoiding “name-space collisions”.
  • 4. PACKAGES Cont... ● Many implementations of Java use a hierarchical file system to manage source and class files. It is easy to organize class files into packages. All we need to do is put related class files in the same directory, give the directory a name that relates to the purpose of the classes, and add a line to the top of each class file that declares the package name, which is the same as the directory name where they reside. ● Types of package: ■ 1) User defined package: The package we create is called user-defined package. ■ 2) Built-in package: The already defined package like java.io.*, java.lang.* etc are known as built-in packages.
  • 6. PACKAGES Cont... Defining a Package: ● This statement should be used in the beginning of the program to include that program in that particular package. package <package name>; ● The package keyword is used to create a package in java. To Compile: javac -d directory javafilename.java To Run: java packagename.classname ● The -d switch specifies the destination where to put the generated class file ● If you want to keep the package within the same directory, you can use . (dot).
  • 7. How to access package from another package? There are three ways to access the package from outside the package. 1. import package.*; 2. import package.classname; 3. fully qualified name. Using packagename.* ● If you use package.* then all the classes and interfaces of this package will be accessible but not subpackages. ● The import keyword is used to make the classes and interface of another package accessible to the current package.
  • 8. Using packagename.classname ● If you import package.classname then only declared class of this package will be accessible. Using fully qualified name ● If you use fully qualified name then only declared class of this package will be accessible. ● Now there is no need to import. But you need to use fully qualified name every time when you are accessing the class or interface. ● It is generally used when two packages have same class name e.g. java.util and java.sql packages contain Date class. Note: If you import a package, all the classes and interface of that package will be imported excluding the classes and interfaces of the subpackages. Hence, you need to import the subpackage as well.
  • 9. Sequence of the program must be package then import then class.
  • 10. Packaging up multiple classes Package with Multiple Public Classes ● A Java source file can have only one class declared as public, we cannot put two or more public classes together in a.java file. This is because of the restriction that the file name should be same as the name of the public class with .javaextension. ● If we want to multiple classes under consideration are to be declared as public, we have to store them in separate source files and attach the package statement as the first statement in those source files. //Save as A.java //Save as B.java package javapoint; package java Public class B{} public class a{}
  • 11. CLASSPATH ● It is a environmental variable, which contains the path for the default-working directory (.). ● The specific location that java compiler will consider, as the root of any package hierarchy is, controlled by Classpath ● The package name is closely associated with the directory structure used to store the classes. The classes (and other entities) belonging to a specific package are stored together in the same directory. ● There is a scenario, I want to put the class file of A.java source file in classes folder of c: drive. For example:
  • 12. //save as Simple.java package mypack; public class Simple{ public static void main(String args[]){ System.out.println("Welcome to package"); } } To Compile: e:sources> javac -d c:classes Simple.java To Run: To run this program from e:source directory, you need to set classpath of the directory where the class file resides. e:sources> set classpath=c:classes; e:sources> java mypack.Simple
  • 13. Another way to run this program by -classpath switch of java: ● The -classpath switch can be used with javac and java tool. ● To run this program from e:source directory, you can use -classpath switch of java that tells where to look for class file. For example: ■ e:sources> java -classpath c:classes mypack.Simple Access Protection in Packages ● Access modifiers define the scope of the class and its members (data and methods). ● There are four categories, provided by Java regarding the visibility of the class members between classes and packages: ■ Subclasses in the same package ■ Non-subclasses in the same package ■ Subclasses in different packages ■ Classes that are neither in the same package nor subclasses