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

Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handlingkamal kotecha
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classesShreyans Pathak
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Michelle Anne Meralpis
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in javaVishnu Suresh
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handlingNahian Ahmed
 
constructors in java ppt
constructors in java pptconstructors in java ppt
constructors in java pptkunal kishore
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++Vishal Patil
 
Introduction to method overloading & method overriding in java hdm
Introduction to method overloading & method overriding  in java  hdmIntroduction to method overloading & method overriding  in java  hdm
Introduction to method overloading & method overriding in java hdmHarshal Misalkar
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaMonika Mishra
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methodsShubham Dwivedi
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in javaTech_MX
 
Interface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar SinghInterface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar Singhdheeraj_cse
 
Access specifiers(modifiers) in java
Access specifiers(modifiers) in javaAccess specifiers(modifiers) in java
Access specifiers(modifiers) in javaHrithikShinde
 
Ppt on this and super keyword
Ppt on this and super keywordPpt on this and super keyword
Ppt on this and super keywordtanu_jaswal
 

What's hot (20)

Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Java exception
Java exception Java exception
Java exception
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classes
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in java
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
 
Packages in java
Packages in javaPackages in java
Packages in java
 
constructors in java ppt
constructors in java pptconstructors in java ppt
constructors in java ppt
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Introduction to method overloading & method overriding in java hdm
Introduction to method overloading & method overriding  in java  hdmIntroduction to method overloading & method overriding  in java  hdm
Introduction to method overloading & method overriding in java hdm
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
 
Chapter 07 inheritance
Chapter 07 inheritanceChapter 07 inheritance
Chapter 07 inheritance
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Interface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar SinghInterface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar Singh
 
Access specifiers(modifiers) in java
Access specifiers(modifiers) in javaAccess specifiers(modifiers) in java
Access specifiers(modifiers) in java
 
Java packages
Java packagesJava packages
Java packages
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Ppt on this and super keyword
Ppt on this and super keywordPpt on this and super keyword
Ppt on this and super keyword
 

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 (8)

Exception handling
Exception handlingException handling
Exception handling
 
Java multi threading
Java multi threadingJava multi threading
Java multi threading
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
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

How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 

Recently uploaded (20)

How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 

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