SlideShare a Scribd company logo
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

Packages and Interfaces
Packages and InterfacesPackages and Interfaces
Packages and Interfaces
AkashDas112
 
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
NithyaN19
 
Distributed Programming (RMI)
Distributed Programming (RMI)Distributed Programming (RMI)
Distributed Programming (RMI)Ravi Kant Sahu
 
Access modifiers in java
Access modifiers in javaAccess modifiers in java
Access modifiers in java
Madishetty Prathibha
 
Cs8392 oops 5 units notes
Cs8392 oops 5 units notes Cs8392 oops 5 units notes
Cs8392 oops 5 units notes
Narayanan sockalinganathan
 
Unit3 part3-packages and interfaces
Unit3 part3-packages and interfacesUnit3 part3-packages and interfaces
Unit3 part3-packages and interfaces
DevaKumari Vijay
 
Java interview questions 2
Java interview questions 2Java interview questions 2
Java interview questions 2
Sherihan Anver
 
java packages
java packagesjava packages
java packages
aptechsravan
 
Object+oriented+programming+in+java
Object+oriented+programming+in+javaObject+oriented+programming+in+java
Object+oriented+programming+in+java
Ye Win
 
Java interface
Java interfaceJava interface
Java interface
Arati Gadgil
 
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
Jyothishmathi Institute of Technology and Science Karimnagar
 
Basic Java Programming
Basic Java ProgrammingBasic Java Programming
Basic Java Programming
Math-Circle
 
Basics of Java
Basics of JavaBasics of Java
Basics of Java
Sherihan Anver
 
Java packages
Java packagesJava packages
Java packages
Jeffrey Quevedo
 
Pi j4.1 packages
Pi j4.1 packagesPi j4.1 packages
Pi j4.1 packages
mcollison
 
Core java notes with examples
Core java notes with examplesCore java notes with examples
Core java notes with examples
bindur87
 

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

Sms several papers
Sms several papersSms several papers
Sms several papersArjun Shanka
 
Questions for Class I & II
Questions for Class I & IIQuestions for Class I & II
Questions for Class I & II
Saloni Jaiswal
 
Classes and Nested Classes in Java
Classes and Nested Classes in JavaClasses and Nested Classes in Java
Classes and Nested Classes in JavaRavi_Kant_Sahu
 

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

packages.ppt
packages.pptpackages.ppt
packages.ppt
SanthiNivas
 
Java - Packages Concepts
Java - Packages ConceptsJava - Packages Concepts
Java - Packages Concepts
Victer Paul
 
Packages in java
Packages in javaPackages in java
Packages in java
Elizabeth alexander
 
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
 
Package in Java
Package in JavaPackage in Java
Package in Java
lalithambiga kamaraj
 
packages.ppt
packages.pptpackages.ppt
packages.ppt
radhika477746
 
packages.ppt
packages.pptpackages.ppt
packages.ppt
surajthakur474818
 
Java package
Java packageJava package
Java package
CS_GDRCST
 
Packages in java
Packages in javaPackages in java
Packages in java
SahithiReddyEtikala
 
Packages
PackagesPackages
Packages
Nuha Noor
 
Javapackages 4th semester
Javapackages 4th semesterJavapackages 4th semester
Javapackages 4th semester
Varendra University Rajshahi-bangladesh
 
Packages
PackagesPackages
Packages
Monika Mishra
 
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
 
packages unit 5 .ppt
packages  unit 5 .pptpackages  unit 5 .ppt
packages unit 5 .ppt
thenmozhip8
 
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
PackagesPackages
Packages in java
Packages in javaPackages in java
Packages in java
Kavitha713564
 

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

String handling(string buffer class)
String handling(string buffer class)String handling(string buffer class)
String handling(string buffer class)Ravi Kant Sahu
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)Ravi Kant Sahu
 
Introduction to Java Programming
Introduction to Java ProgrammingIntroduction to Java Programming
Introduction to Java Programming
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

Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
nkrafacyberclub
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
Ralf Eggert
 
GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...
ThomasParaiso2
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
James Anderson
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
DianaGray10
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Nexer Digital
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 

Recently uploaded (20)

Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 

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)