SlideShare a Scribd company logo
1 of 14
JAVA

PACKAGES
Packages
  Package is a container for classes
  A package is a grouping of related types
(classes and interfaces) providing access
protection and name space management.
  In simple words, packages is the way we
organize files into different directories according
to their functionality, usability as well as category
they should belong to.
Packages
The JDK package from SUN: java.net
Why do we need Packages?
• One can easily determine that these types are related.
• One knows where to find types that can provide task-
related functions.
• The names of your types won't conflict with the type
names in other packages because the package creates a
new namespace.
• One can allow types within the package to have
unrestricted access to one another yet still restrict access
for types outside the package.
Java files for graphics
//in the Shape.java file
public abstract class Shape {
    . . .
}
//in the Circle.java file
public class Circle extends Shape {
    . . .
}
//in the Rectangle.java file
public class Rectangle extends Shape {
    . . .
}
Package graphics: 1st step
• Choose a name for the package (graphics, for example)
                                 graphics
and put a package statement with that name at the top of
every source file that contains the classes that you want to
include in the package.
• In the Shape.java file:
package graphics;
public abstract class Shape {
      . . .
}
…
• In the Rectangle.java file:
package graphics;
public class Rectangle extends Shape
{
      . . .
}
Package graphics: 2nd step
Put the source files in a directory whose name (graphics,
for example) reflects the name of the package to which
the type belongs:
...graphicsShape.java
...graphicsCircle.java
...graphicsRectangle.java
...graphicsCylinder.java
etc.
Package Name & Package Folder
1) A company uses its reversed Internet domain name for its package
names. The ABC company, whose Internet domain name is
ABC.com, would precede all its package names with com.ABC

       package com.ABC.graphics;
2) Each component of the package name corresponds to a
subdirectory. So, if the ABC company had a com.ABC.graphics
package that contained a Shape.java source file, it would be contained
in a series of subdirectories like this:

    ....comABCgraphicsShape.java
    ....comABCgraphicsCircle.java
                 . . .
How to use packages
1. Referring to a package member by its qualified name:
graphics.Circle myCircle = new graphics.Circle();

2. Importing a package member:
import graphics.Circle;
...
Circle myCircle = new Circle();
graphics.Rectangle myR = new graphics.Rectangle();

3. Importing an entire package:
import graphics.*;
...
Circle myCircle = new Circle();
Rectangle myRectangle = new Rectangle();
Access to members of the classes
                                            private   (default)   protected   public
                  The same class             Yes        Yes         Yes        Yes
              Subclass in the package         No        Yes         Yes        Yes
            Non-subclass in the package       No        Yes         Yes        Yes
            Subclass in another package       No        No          Yes        Yes
          Non-subclass in another package     No        No           No        Yes




Java packages can be stored in compressed files called JAR files,
allowing classes to download faster as a group rather than one at a time.
Access modifier private
package p1               package p2

 class C1                 class C2 extends C1

   private int x            x cannot be read or
                            modified in C2




 class C3                 class C4

   C1 c1;                   C1 c1;
   c1.x cannot be read      c1.x cannot be read
   or modified              nor modified
Default access modifier
package p1               package p2

 class C1                 class C2 extends C1

   int x                    x cannot be read or
                            modified in C2




 class C3                 class C4

   C1 c1;                   C1 c1;
   c1.x can be read or      c1.x cannot be read
   modified                 nor modified
Access modifier protected
  package p1               package p2

   class C1                 class C2 extends C1

     protected int x          x can be read or
                              modified in C2




   class C3                 class C4

     C1 c1;                   C1 c1;
     c1.x can be read or      c1.x cannot be read
     modified                 nor modified
Access modifier public
package p1               package p2

 class C1                 class C2 extends C1

   public int x             x can be read or
                            modified in C2




 class C3                 class C4

   C1 c1;                   C1 c1;
   c1.x can be read or      c1.x can be read nor
   modified                 modified

More Related Content

What's hot

What's hot (20)

Packages in java
Packages in javaPackages in java
Packages in java
 
Access specifiers(modifiers) in java
Access specifiers(modifiers) in javaAccess specifiers(modifiers) in java
Access specifiers(modifiers) in java
 
Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread Synchronization
 
Inner classes in java
Inner classes in javaInner classes in java
Inner classes in java
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Abstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core JavaAbstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core Java
 
Abstract class
Abstract classAbstract class
Abstract class
 
Java oops PPT
Java oops PPTJava oops PPT
Java oops PPT
 
Introduction to package in java
Introduction to package in javaIntroduction to package in java
Introduction to package in java
 
Encapsulation
EncapsulationEncapsulation
Encapsulation
 
Applets in java
Applets in javaApplets in java
Applets in java
 
Packages in java
Packages in javaPackages in java
Packages in java
 
[OOP - Lec 08] Encapsulation (Information Hiding)
[OOP - Lec 08] Encapsulation (Information Hiding)[OOP - Lec 08] Encapsulation (Information Hiding)
[OOP - Lec 08] Encapsulation (Information Hiding)
 
Java applet
Java appletJava applet
Java applet
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in java
 
C# Inheritance
C# InheritanceC# Inheritance
C# Inheritance
 
[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 

Viewers also liked

Network Security Primer
Network Security PrimerNetwork Security Primer
Network Security PrimerVenkatesh Iyer
 
Packages and inbuilt classes of java
Packages and inbuilt classes of javaPackages and inbuilt classes of java
Packages and inbuilt classes of javakamal kotecha
 
Email and web security
Email and web securityEmail and web security
Email and web securityshahhardik27
 
Microsoft Hololens
Microsoft Hololens Microsoft Hololens
Microsoft Hololens arun alfie
 
Threats to information security
Threats to information securityThreats to information security
Threats to information securityarun alfie
 
Graphics programming in Java
Graphics programming in JavaGraphics programming in Java
Graphics programming in JavaTushar B Kute
 
Intrusion detection system
Intrusion detection system Intrusion detection system
Intrusion detection system gaurav koriya
 
Intrusion detection system ppt
Intrusion detection system pptIntrusion detection system ppt
Intrusion detection system pptSheetal Verma
 
Information security: importance of having defined policy & process
Information security: importance of having defined policy & processInformation security: importance of having defined policy & process
Information security: importance of having defined policy & processInformation Technology Society Nepal
 
Threats to Information Resources - MIS - Shimna
Threats to Information Resources - MIS - ShimnaThreats to Information Resources - MIS - Shimna
Threats to Information Resources - MIS - ShimnaChinnu Shimna
 
Importance Of A Security Policy
Importance Of A Security PolicyImportance Of A Security Policy
Importance Of A Security Policycharlesgarrett
 
Email security - Netwroking
Email security - Netwroking Email security - Netwroking
Email security - Netwroking Salman Memon
 
Computer security threats & prevention
Computer security threats & preventionComputer security threats & prevention
Computer security threats & preventionPriSim
 

Viewers also liked (16)

pgp s mime
pgp s mimepgp s mime
pgp s mime
 
Email Security
Email SecurityEmail Security
Email Security
 
Network Security Primer
Network Security PrimerNetwork Security Primer
Network Security Primer
 
Packages and inbuilt classes of java
Packages and inbuilt classes of javaPackages and inbuilt classes of java
Packages and inbuilt classes of java
 
Email and web security
Email and web securityEmail and web security
Email and web security
 
Microsoft Hololens
Microsoft Hololens Microsoft Hololens
Microsoft Hololens
 
Threats to information security
Threats to information securityThreats to information security
Threats to information security
 
Graphics programming in Java
Graphics programming in JavaGraphics programming in Java
Graphics programming in Java
 
Intrusion detection system
Intrusion detection system Intrusion detection system
Intrusion detection system
 
Intrusion detection system ppt
Intrusion detection system pptIntrusion detection system ppt
Intrusion detection system ppt
 
Information security: importance of having defined policy & process
Information security: importance of having defined policy & processInformation security: importance of having defined policy & process
Information security: importance of having defined policy & process
 
Threats to Information Resources - MIS - Shimna
Threats to Information Resources - MIS - ShimnaThreats to Information Resources - MIS - Shimna
Threats to Information Resources - MIS - Shimna
 
Importance Of A Security Policy
Importance Of A Security PolicyImportance Of A Security Policy
Importance Of A Security Policy
 
Email security - Netwroking
Email security - Netwroking Email security - Netwroking
Email security - Netwroking
 
Computer security threats & prevention
Computer security threats & preventionComputer security threats & prevention
Computer security threats & prevention
 
Digital signature
Digital signatureDigital signature
Digital signature
 

Similar to Java packages

5.interface and packages
5.interface and packages5.interface and packages
5.interface and packagesDeepak Sharma
 
Unit4 java
Unit4 javaUnit4 java
Unit4 javamrecedu
 
packages in java & c++
packages in java & c++packages in java & c++
packages in java & c++pankaj chelak
 
Lecture 9 access modifiers and packages
Lecture   9 access modifiers and packagesLecture   9 access modifiers and packages
Lecture 9 access modifiers and packagesmanish kumar
 
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
 
OOPs with Java - Packaging and Access Modifiers
OOPs with Java - Packaging and Access Modifiers OOPs with Java - Packaging and Access Modifiers
OOPs with Java - Packaging and Access Modifiers Hitesh-Java
 
Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.MASQ Technologies
 
Z blue interfaces and packages (37129912)
Z blue   interfaces and  packages (37129912)Z blue   interfaces and  packages (37129912)
Z blue interfaces and packages (37129912)Narayana Swamy
 
Session 11 - OOP's with Java - Packaging and Access Modifiers
Session 11 - OOP's with Java - Packaging and Access ModifiersSession 11 - OOP's with Java - Packaging and Access Modifiers
Session 11 - OOP's with Java - Packaging and Access ModifiersPawanMM
 

Similar to Java packages (20)

Javapackages 4th semester
Javapackages 4th semesterJavapackages 4th semester
Javapackages 4th semester
 
5.interface and packages
5.interface and packages5.interface and packages
5.interface and packages
 
Java packages oop
Java packages oopJava packages oop
Java packages oop
 
Unit4 java
Unit4 javaUnit4 java
Unit4 java
 
packages in java & c++
packages in java & c++packages in java & c++
packages in java & c++
 
Lecture 9 access modifiers and packages
Lecture   9 access modifiers and packagesLecture   9 access modifiers and packages
Lecture 9 access modifiers and packages
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
 
Package in Java
Package in JavaPackage in Java
Package in Java
 
Unit 2 notes.pdf
Unit 2 notes.pdfUnit 2 notes.pdf
Unit 2 notes.pdf
 
Packages
PackagesPackages
Packages
 
Lecture 19
Lecture 19Lecture 19
Lecture 19
 
7.Packages and Interfaces(MB).ppt .
7.Packages and Interfaces(MB).ppt             .7.Packages and Interfaces(MB).ppt             .
7.Packages and Interfaces(MB).ppt .
 
00ps inheritace using c++
00ps inheritace using c++00ps inheritace using c++
00ps inheritace using c++
 
Classes & Interfaces
Classes & InterfacesClasses & Interfaces
Classes & Interfaces
 
OOPs with Java - Packaging and Access Modifiers
OOPs with Java - Packaging and Access Modifiers OOPs with Java - Packaging and Access Modifiers
OOPs with Java - Packaging and Access Modifiers
 
Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.
 
Unit 4 Java
Unit 4 JavaUnit 4 Java
Unit 4 Java
 
Java access modifiers
Java access modifiersJava access modifiers
Java access modifiers
 
Z blue interfaces and packages (37129912)
Z blue   interfaces and  packages (37129912)Z blue   interfaces and  packages (37129912)
Z blue interfaces and packages (37129912)
 
Session 11 - OOP's with Java - Packaging and Access Modifiers
Session 11 - OOP's with Java - Packaging and Access ModifiersSession 11 - OOP's with Java - Packaging and Access Modifiers
Session 11 - OOP's with Java - Packaging and Access Modifiers
 

More from Raja Sekhar

Exception handling
Exception handlingException handling
Exception handlingRaja Sekhar
 
Java multi threading
Java multi threadingJava multi threading
Java multi threadingRaja Sekhar
 
String handling session 5
String handling session 5String handling session 5
String handling session 5Raja Sekhar
 
java Basic Programming Needs
java Basic Programming Needsjava Basic Programming Needs
java Basic Programming NeedsRaja Sekhar
 
Class object method constructors in java
Class object method constructors in javaClass object method constructors in java
Class object method constructors in javaRaja Sekhar
 
Java OOP s concepts and buzzwords
Java OOP s concepts and buzzwordsJava OOP s concepts and buzzwords
Java OOP s concepts and buzzwordsRaja Sekhar
 

More from Raja Sekhar (7)

Exception handling
Exception handlingException handling
Exception handling
 
Java multi threading
Java multi threadingJava multi threading
Java multi threading
 
String handling session 5
String handling session 5String handling session 5
String handling session 5
 
java Basic Programming Needs
java Basic Programming Needsjava Basic Programming Needs
java Basic Programming Needs
 
Class object method constructors in java
Class object method constructors in javaClass object method constructors in java
Class object method constructors in java
 
Java OOP s concepts and buzzwords
Java OOP s concepts and buzzwordsJava OOP s concepts and buzzwords
Java OOP s concepts and buzzwords
 
Java Starting
Java StartingJava Starting
Java Starting
 

Recently uploaded

ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfOverkill Security
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 

Recently uploaded (20)

ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 

Java packages

  • 2. Packages Package is a container for classes A package is a grouping of related types (classes and interfaces) providing access protection and name space management. In simple words, packages is the way we organize files into different directories according to their functionality, usability as well as category they should belong to.
  • 3. Packages The JDK package from SUN: java.net
  • 4. Why do we need Packages? • One can easily determine that these types are related. • One knows where to find types that can provide task- related functions. • The names of your types won't conflict with the type names in other packages because the package creates a new namespace. • One can allow types within the package to have unrestricted access to one another yet still restrict access for types outside the package.
  • 5. Java files for graphics //in the Shape.java file public abstract class Shape { . . . } //in the Circle.java file public class Circle extends Shape { . . . } //in the Rectangle.java file public class Rectangle extends Shape { . . . }
  • 6. Package graphics: 1st step • Choose a name for the package (graphics, for example) graphics and put a package statement with that name at the top of every source file that contains the classes that you want to include in the package. • In the Shape.java file: package graphics; public abstract class Shape { . . . } … • In the Rectangle.java file: package graphics; public class Rectangle extends Shape { . . . }
  • 7. Package graphics: 2nd step Put the source files in a directory whose name (graphics, for example) reflects the name of the package to which the type belongs: ...graphicsShape.java ...graphicsCircle.java ...graphicsRectangle.java ...graphicsCylinder.java etc.
  • 8. Package Name & Package Folder 1) A company uses its reversed Internet domain name for its package names. The ABC company, whose Internet domain name is ABC.com, would precede all its package names with com.ABC package com.ABC.graphics; 2) Each component of the package name corresponds to a subdirectory. So, if the ABC company had a com.ABC.graphics package that contained a Shape.java source file, it would be contained in a series of subdirectories like this: ....comABCgraphicsShape.java ....comABCgraphicsCircle.java . . .
  • 9. How to use packages 1. Referring to a package member by its qualified name: graphics.Circle myCircle = new graphics.Circle(); 2. Importing a package member: import graphics.Circle; ... Circle myCircle = new Circle(); graphics.Rectangle myR = new graphics.Rectangle(); 3. Importing an entire package: import graphics.*; ... Circle myCircle = new Circle(); Rectangle myRectangle = new Rectangle();
  • 10. Access to members of the classes private (default) protected public The same class Yes Yes Yes Yes Subclass in the package No Yes Yes Yes Non-subclass in the package No Yes Yes Yes Subclass in another package No No Yes Yes Non-subclass in another package No No No Yes Java packages can be stored in compressed files called JAR files, allowing classes to download faster as a group rather than one at a time.
  • 11. Access modifier private package p1 package p2 class C1 class C2 extends C1 private int x x cannot be read or modified in C2 class C3 class C4 C1 c1; C1 c1; c1.x cannot be read c1.x cannot be read or modified nor modified
  • 12. Default access modifier package p1 package p2 class C1 class C2 extends C1 int x x cannot be read or modified in C2 class C3 class C4 C1 c1; C1 c1; c1.x can be read or c1.x cannot be read modified nor modified
  • 13. Access modifier protected package p1 package p2 class C1 class C2 extends C1 protected int x x can be read or modified in C2 class C3 class C4 C1 c1; C1 c1; c1.x can be read or c1.x cannot be read modified nor modified
  • 14. Access modifier public package p1 package p2 class C1 class C2 extends C1 public int x x can be read or modified in C2 class C3 class C4 C1 c1; C1 c1; c1.x can be read or c1.x can be read nor modified modified