SlideShare a Scribd company logo
2
Most read
4
Most read
10
Most read
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

PPTX
Packages,static,this keyword in java
PDF
Java I/o streams
PPT
Abstract class in java
PPT
Exception Handling in JAVA
PPTX
Access modifiers in java
PPTX
Java- Nested Classes
PPT
Packages and interfaces
PPTX
java interface and packages
Packages,static,this keyword in java
Java I/o streams
Abstract class in java
Exception Handling in JAVA
Access modifiers in java
Java- Nested Classes
Packages and interfaces
java interface and packages

What's hot (20)

PPTX
Polymorphism in java
PPTX
Super Keyword in Java.pptx
PPT
friend function(c++)
PDF
Java IO
PDF
Java variable types
PPTX
Encapsulation
PPT
Packages in java
PPTX
Packages
PPTX
Inner classes in java
PPTX
Control Statements in Java
PPTX
Java exception handling
PPT
Constructor
PDF
Java - Exception Handling Concepts
PPTX
Abstraction in java.pptx
PPTX
Control statements in java
PPTX
Packages in java
PPTX
This keyword in java
PPSX
Exception Handling
PPTX
Methods in java
PPTX
I/O Streams
Polymorphism in java
Super Keyword in Java.pptx
friend function(c++)
Java IO
Java variable types
Encapsulation
Packages in java
Packages
Inner classes in java
Control Statements in Java
Java exception handling
Constructor
Java - Exception Handling Concepts
Abstraction in java.pptx
Control statements in java
Packages in java
This keyword in java
Exception Handling
Methods in java
I/O Streams
Ad

Viewers also liked (16)

PPTX
pgp s mime
PPSX
Email Security
PPT
Network Security Primer
PPS
Packages and inbuilt classes of java
PPT
Email and web security
PPTX
Microsoft Hololens
PPTX
Threats to information security
PPT
Graphics programming in Java
PPSX
Intrusion detection system
PPT
Intrusion detection system ppt
PPTX
Information security: importance of having defined policy & process
PPTX
Threats to Information Resources - MIS - Shimna
PPTX
Importance Of A Security Policy
PPTX
Email security - Netwroking
PPTX
Computer security threats & prevention
PPT
Digital signature
pgp s mime
Email Security
Network Security Primer
Packages and inbuilt classes of java
Email and web security
Microsoft Hololens
Threats to information security
Graphics programming in Java
Intrusion detection system
Intrusion detection system ppt
Information security: importance of having defined policy & process
Threats to Information Resources - MIS - Shimna
Importance Of A Security Policy
Email security - Netwroking
Computer security threats & prevention
Digital signature
Ad

Similar to Java packages (20)

PPT
Javapackages 4th semester
PPTX
5.interface and packages
PPTX
Java packages oop
DOCX
Unit4 java
PPTX
packages in java & c++
PPTX
Lecture 9 access modifiers and packages
PPTX
C++ presentation
PPTX
Package in Java
PDF
Unit 2 notes.pdf
PPTX
Packages
PPTX
java package in java.. in java packages.
PPTX
java package java package in java packages
PPTX
Java package
PPTX
Lecture 19
PPT
PACKAGE.PPT12345678912345949745654646455
PPT
7.Packages and Interfaces(MB).ppt .
PPTX
javapackage,try,cthrow,finallytch,-160518085421 (1).pptx
PPTX
00ps inheritace using c++
PPTX
packages in java object oriented programming
PPT
Classes & Interfaces
Javapackages 4th semester
5.interface and packages
Java packages oop
Unit4 java
packages in java & c++
Lecture 9 access modifiers and packages
C++ presentation
Package in Java
Unit 2 notes.pdf
Packages
java package in java.. in java packages.
java package java package in java packages
Java package
Lecture 19
PACKAGE.PPT12345678912345949745654646455
7.Packages and Interfaces(MB).ppt .
javapackage,try,cthrow,finallytch,-160518085421 (1).pptx
00ps inheritace using c++
packages in java object oriented programming
Classes & Interfaces

More from Raja Sekhar (8)

PPT
Exception handling
PPT
Java multi threading
PPT
Java interfaces
PPT
String handling session 5
PPTX
java Basic Programming Needs
PPTX
Class object method constructors in java
PPT
Java OOP s concepts and buzzwords
PPTX
Java Starting
Exception handling
Java multi threading
Java interfaces
String handling session 5
java Basic Programming Needs
Class object method constructors in java
Java OOP s concepts and buzzwords
Java Starting

Recently uploaded (20)

PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Approach and Philosophy of On baking technology
PPTX
Spectroscopy.pptx food analysis technology
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Machine Learning_overview_presentation.pptx
PDF
Empathic Computing: Creating Shared Understanding
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PPTX
OMC Textile Division Presentation 2021.pptx
PDF
August Patch Tuesday
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
A comparative study of natural language inference in Swahili using monolingua...
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPT
Teaching material agriculture food technology
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
A Presentation on Artificial Intelligence
A comparative analysis of optical character recognition models for extracting...
Approach and Philosophy of On baking technology
Spectroscopy.pptx food analysis technology
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Machine Learning_overview_presentation.pptx
Empathic Computing: Creating Shared Understanding
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Accuracy of neural networks in brain wave diagnosis of schizophrenia
Univ-Connecticut-ChatGPT-Presentaion.pdf
OMC Textile Division Presentation 2021.pptx
August Patch Tuesday
Mobile App Security Testing_ A Comprehensive Guide.pdf
A comparative study of natural language inference in Swahili using monolingua...
Per capita expenditure prediction using model stacking based on satellite ima...
Encapsulation_ Review paper, used for researhc scholars
Reach Out and Touch Someone: Haptics and Empathic Computing
Teaching material agriculture food technology
NewMind AI Weekly Chronicles - August'25-Week II
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
A Presentation on Artificial Intelligence

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