SlideShare a Scribd company logo
1 of 15
Download to read offline
Programming in Java
Lecture 14: Packages
By
Ravi Kant Sahu
Asst. Professor
Lovely Professional University, PunjabLovely Professional University, Punjab
Introduction
What is Package?
• A package is a namespace that organizes a set of related classes
and interfaces.
Or
• A Java package is a mechanism for organizing Java classes into
namespaces.
• A package is a logical container of classes, interfaces and sub-
packages.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Package
• Package provides unique namespace for classes (i.e. facility of
uniquely identifying a class or interface and resolving name
conflicts).
• If a class or interface is stored in a package, then it is
referenced by using the package name.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Introduction
What is the need of Package?
• A unique name should be used for each class to avoid name
collisions.
• Without some way to manage the name space, we could run out
of convenient, descriptive names for individual classes.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Reasons to use packages
• Grouping related classes.
• In large programs prevents name conflicts.
• Allows effective use of default "package" visibility.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Defining a Package
• Each file may have a package declaration which precedes all
non-comment code.
• The package name must be the same as the enclosing
directory.
• Default package:
If a package declaration is omitted, all
classes in that directory are said to belong to the "default"
package.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Defining a Package
• package Package_Name;
must be the first statement in a class or interface definition.
Example:
package P1;
public class Test
{
public static void main(String arr[])
{
System.out.println(“This is package P1”);
}
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Compilation:
Package P1> javac Test.java
Execution:
Package> java P1.Test
Note: To execute the class file of a package, we need to go back
to the parent folder of package and refer the class as-
package_name.class_name
• We can create the package while compiling the program.
javac –d . Test.java
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
classes in different packages
package P1;
public class Test11
{ public void display()
{System.out.println(“This is package P1”);}
}
package P2;
import P1.Test11;
public class Test1 {
public static void main(String arr[]) {
P1.Test 11 t = new P1.Test11();
t.display(); }
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
• ‘import’ keyword is used to import the classes of one package
into another package.
import package_name.*;
• A class must be publically defined in the package in order to
be imported in other package.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Importing a Package
• Once a class is imported in any package, it can be referenced
without package name.
• If a class is imported but not referenced in importing class,
then it will not be loaded (i.e. the imported class will be
loaded only when it is referenced ).
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Core Packages in Java
java.lang : basic language functionality and fundamental types
java.util : collection data structure classes
java.io : file operations
java.math : multiprecision arithmetics
java.nio : the New I/O framework for Java
java.net : networking operations, sockets
java.security : key generation, encryption and decryption
java.sql : Java Database Connectivity (JDBC) to access databases
java.awt : basic hierarchy of packages for native GUI components
javax.swing : hierarchy of packages for platform-independent rich
GUI components
java.applet : classes for creating an applet
Note: The java.lang package is available without the use of an import
statement.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Access Specifiers
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Access Specifiers
Specifier Sub class
(Same
Package)
Non-Sub class
(Same
Package)
Sub class
(Different
Package)
Non-Sub class
(Different
Package)
Public Yes Yes Yes Yes
Protected Yes Yes Yes No
default Yes Yes No No
Private No No No No
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

More Related Content

What's hot

Distributed Programming (RMI)
Distributed Programming (RMI)Distributed Programming (RMI)
Distributed Programming (RMI)
Ravi Kant Sahu
 

What's hot (20)

Packages and Interfaces
Packages and InterfacesPackages and Interfaces
Packages and Interfaces
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
 
Distributed Programming (RMI)
Distributed Programming (RMI)Distributed Programming (RMI)
Distributed Programming (RMI)
 
Access modifiers in java
Access modifiers in javaAccess modifiers in java
Access modifiers in java
 
Cs8392 oops 5 units notes
Cs8392 oops 5 units notes Cs8392 oops 5 units notes
Cs8392 oops 5 units notes
 
Unit3 part3-packages and interfaces
Unit3 part3-packages and interfacesUnit3 part3-packages and interfaces
Unit3 part3-packages and interfaces
 
Java interview questions 2
Java interview questions 2Java interview questions 2
Java interview questions 2
 
java packages
java packagesjava packages
java packages
 
Java access modifiers
Java access modifiersJava access modifiers
Java access modifiers
 
Object+oriented+programming+in+java
Object+oriented+programming+in+javaObject+oriented+programming+in+java
Object+oriented+programming+in+java
 
Java interface
Java interfaceJava interface
Java interface
 
Packages and interfaces
Packages and interfacesPackages and interfaces
Packages and interfaces
 
JAVA PROGRAMMING – Packages - Stream based I/O
JAVA PROGRAMMING – Packages - Stream based I/O JAVA PROGRAMMING – Packages - Stream based I/O
JAVA PROGRAMMING – Packages - Stream based I/O
 
Basic Java Programming
Basic Java ProgrammingBasic Java Programming
Basic Java Programming
 
Basics of Java
Basics of JavaBasics of Java
Basics of Java
 
Java packages
Java packagesJava packages
Java packages
 
Unit 3 Java
Unit 3 JavaUnit 3 Java
Unit 3 Java
 
Java packages
Java packagesJava packages
Java packages
 
Pi j4.1 packages
Pi j4.1 packagesPi j4.1 packages
Pi j4.1 packages
 
Core java notes with examples
Core java notes with examplesCore java notes with examples
Core java notes with examples
 

Viewers also liked (14)

Event handling
Event handlingEvent handling
Event handling
 
Sms several papers
Sms several papersSms several papers
Sms several papers
 
Networking
NetworkingNetworking
Networking
 
Jun 2012(1)
Jun 2012(1)Jun 2012(1)
Jun 2012(1)
 
List classes
List classesList classes
List classes
 
Collection framework
Collection frameworkCollection framework
Collection framework
 
Questions for Class I & II
Questions for Class I & IIQuestions for Class I & II
Questions for Class I & II
 
Basic IO
Basic IOBasic IO
Basic IO
 
Swing api
Swing apiSwing api
Swing api
 
Jdbc
JdbcJdbc
Jdbc
 
Multi threading
Multi threadingMulti threading
Multi threading
 
Servlets
ServletsServlets
Servlets
 
Classes and Nested Classes in Java
Classes and Nested Classes in JavaClasses and Nested Classes in Java
Classes and Nested Classes in Java
 
Generics
GenericsGenerics
Generics
 

Similar to Packages

Similar to Packages (20)

packages.ppt
packages.pptpackages.ppt
packages.ppt
 
Java - Packages Concepts
Java - Packages ConceptsJava - Packages Concepts
Java - Packages Concepts
 
Packages in java
Packages in javaPackages in java
Packages in java
 
javapackage
javapackagejavapackage
javapackage
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Package in Java
Package in JavaPackage in Java
Package in Java
 
packages.ppt
packages.pptpackages.ppt
packages.ppt
 
packages.ppt
packages.pptpackages.ppt
packages.ppt
 
Java package
Java packageJava package
Java package
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Packages
PackagesPackages
Packages
 
Javapackages 4th semester
Javapackages 4th semesterJavapackages 4th semester
Javapackages 4th semester
 
Packages
PackagesPackages
Packages
 
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
 
packages unit 5 .ppt
packages  unit 5 .pptpackages  unit 5 .ppt
packages unit 5 .ppt
 
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
PackagesPackages
Packages
 
Packages in java
Packages in javaPackages in java
Packages in java
 
9 cm604.26
9 cm604.269 cm604.26
9 cm604.26
 

More from Ravi Kant Sahu

More from Ravi Kant Sahu (6)

Java beans
Java beansJava beans
Java beans
 
Applets
AppletsApplets
Applets
 
Exception handling
Exception handlingException handling
Exception handling
 
String handling(string buffer class)
String handling(string buffer class)String handling(string buffer class)
String handling(string buffer class)
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
 
Introduction to Java Programming
Introduction to Java ProgrammingIntroduction to Java Programming
Introduction to Java Programming
 

Recently uploaded

Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Peter Udo Diehl
 

Recently uploaded (20)

Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdfSimplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
 
Intro in Product Management - Коротко про професію продакт менеджера
Intro in Product Management - Коротко про професію продакт менеджераIntro in Product Management - Коротко про професію продакт менеджера
Intro in Product Management - Коротко про професію продакт менеджера
 
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
 
Enterprise Knowledge Graphs - Data Summit 2024
Enterprise Knowledge Graphs - Data Summit 2024Enterprise Knowledge Graphs - Data Summit 2024
Enterprise Knowledge Graphs - Data Summit 2024
 
ECS 2024 Teams Premium - Pretty Secure
ECS 2024   Teams Premium - Pretty SecureECS 2024   Teams Premium - Pretty Secure
ECS 2024 Teams Premium - Pretty Secure
 
WebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM PerformanceWebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM Performance
 
Strategic AI Integration in Engineering Teams
Strategic AI Integration in Engineering TeamsStrategic AI Integration in Engineering Teams
Strategic AI Integration in Engineering Teams
 
The UX of Automation by AJ King, Senior UX Researcher, Ocado
The UX of Automation by AJ King, Senior UX Researcher, OcadoThe UX of Automation by AJ King, Senior UX Researcher, Ocado
The UX of Automation by AJ King, Senior UX Researcher, Ocado
 
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
 
AI presentation and introduction - Retrieval Augmented Generation RAG 101
AI presentation and introduction - Retrieval Augmented Generation RAG 101AI presentation and introduction - Retrieval Augmented Generation RAG 101
AI presentation and introduction - Retrieval Augmented Generation RAG 101
 
Speed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in MinutesSpeed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in Minutes
 
Top 10 Symfony Development Companies 2024
Top 10 Symfony Development Companies 2024Top 10 Symfony Development Companies 2024
Top 10 Symfony Development Companies 2024
 
Connecting the Dots in Product Design at KAYAK
Connecting the Dots in Product Design at KAYAKConnecting the Dots in Product Design at KAYAK
Connecting the Dots in Product Design at KAYAK
 
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone KomSalesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
 
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdfWhere to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
 
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdfThe Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
 
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya HalderCustom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
 
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
 
THE BEST IPTV in GERMANY for 2024: IPTVreel
THE BEST IPTV in  GERMANY for 2024: IPTVreelTHE BEST IPTV in  GERMANY for 2024: IPTVreel
THE BEST IPTV in GERMANY for 2024: IPTVreel
 

Packages

  • 1. Programming in Java Lecture 14: Packages By Ravi Kant Sahu Asst. Professor Lovely Professional University, PunjabLovely Professional University, Punjab
  • 2. Introduction What is Package? • A package is a namespace that organizes a set of related classes and interfaces. Or • A Java package is a mechanism for organizing Java classes into namespaces. • A package is a logical container of classes, interfaces and sub- packages. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 3. Package • Package provides unique namespace for classes (i.e. facility of uniquely identifying a class or interface and resolving name conflicts). • If a class or interface is stored in a package, then it is referenced by using the package name. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 4. Introduction What is the need of Package? • A unique name should be used for each class to avoid name collisions. • Without some way to manage the name space, we could run out of convenient, descriptive names for individual classes. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 5. Reasons to use packages • Grouping related classes. • In large programs prevents name conflicts. • Allows effective use of default "package" visibility. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 6. Defining a Package • Each file may have a package declaration which precedes all non-comment code. • The package name must be the same as the enclosing directory. • Default package: If a package declaration is omitted, all classes in that directory are said to belong to the "default" package. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 7. Defining a Package • package Package_Name; must be the first statement in a class or interface definition. Example: package P1; public class Test { public static void main(String arr[]) { System.out.println(“This is package P1”); } } Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 8. Compilation: Package P1> javac Test.java Execution: Package> java P1.Test Note: To execute the class file of a package, we need to go back to the parent folder of package and refer the class as- package_name.class_name • We can create the package while compiling the program. javac –d . Test.java Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 9. classes in different packages package P1; public class Test11 { public void display() {System.out.println(“This is package P1”);} } package P2; import P1.Test11; public class Test1 { public static void main(String arr[]) { P1.Test 11 t = new P1.Test11(); t.display(); } } Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 10. • ‘import’ keyword is used to import the classes of one package into another package. import package_name.*; • A class must be publically defined in the package in order to be imported in other package. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India) Importing a Package
  • 11. • Once a class is imported in any package, it can be referenced without package name. • If a class is imported but not referenced in importing class, then it will not be loaded (i.e. the imported class will be loaded only when it is referenced ). Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 12. Core Packages in Java java.lang : basic language functionality and fundamental types java.util : collection data structure classes java.io : file operations java.math : multiprecision arithmetics java.nio : the New I/O framework for Java java.net : networking operations, sockets java.security : key generation, encryption and decryption java.sql : Java Database Connectivity (JDBC) to access databases java.awt : basic hierarchy of packages for native GUI components javax.swing : hierarchy of packages for platform-independent rich GUI components java.applet : classes for creating an applet Note: The java.lang package is available without the use of an import statement. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 13. Access Specifiers Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 14. Access Specifiers Specifier Sub class (Same Package) Non-Sub class (Same Package) Sub class (Different Package) Non-Sub class (Different Package) Public Yes Yes Yes Yes Protected Yes Yes Yes No default Yes Yes No No Private No No No No Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 15. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)