SlideShare a Scribd company logo
PACKAGES
By,
M.SHARMILA DEVI M.Sc(IT)
Introduction
❖ The main features of OOP is its ability to reuse the code
already created,
➢ One way of achieving this is by the use of packages
❖ Packages are Java’s way of grouping a variety of classes or
interfaces together.
❖ Packages act as “containers” for classes.
Benefits
❖ The classes contained in the packages of other
programs can be reused.
❖ In packages, classes can be unique compared with
classes in other packages.
❖ Packages provides a way to hide classes.
Java API Packages
❖ Java API provides a large number of classes
grouped into different packages according to
functionality
❖ Most of the time we use the packages available
with the Java API.
Functional breakdown of packages
Java System Packages and Their Classes
Using System Packages
Hierarchical representation of java.awt package
❖ The packages are organised in a hierarchical structure.
❖ It show package name java contains the package awt, which in
turn contains various classes required for implementing
graphical user interface.
❖ There are two ways of accessing the classes stored in a
package.
■ Fully qualified class name
■ Import statements
❖ The first approach is to use the fully qualified class name of the class we
want to use.
java.awt.Colour
❖ Awt is a package within the package java and the hierarchy is represented
by separating the levels with dots.
Import packagename.classname;
Or
Import packagename.*;
❖ These are known as import statements and must appear at the top of the file
before any class declarations.
Import java.awt.Color;
❖ This statement allows the specified class in the specified package to
be imported.
❖ The above statement imports the class Colour and therefore the class
name can now be directly used in the program.
Import java.awt.*;
❖ This statement imports every class contained in the specified
package.
❖ It will bring all classes of java.awt package.
Naming Conventions
❖ Packages can be named using the standard Java naming rules.
❖ Packages should begin with lowercase letters by convention.
❖ It distinguish package names from class names.
Creating Packages
❖ We must first declare the name of the package using the package keyword
followed by a package name.
❖ This must be the first statement in a Java source file.
Example
Accessing a Package
❖ Java system package can be accessed
➢ Using a fully qualified class name.
Or
➢ Using a shortcut approach through the import statement.
❖ The same approaches can be used to access the user defined
packages as well.
❖ The general form of import statement for searching a class is as follows:
❖ Package 1 is the name of the top level package, package2 is the name of the
package that is inside the package1, and so on. Finally, the explicit
classname is specified.
❖ Another approach as follows:
■ Import packagename.*;
❖ The star(*) indicates that the compiler should search entire package
hierarchy when it encounters a class name.
Using a Package
It shows a package named package1 containing a single ClassA
This program that imports the class ClassA from the package package1.
Let us consider another package named package2 containing again a single
class.
In this program, it uses classes contained in both the packages. So it imports
package1 and package2.
The output will be
Class A
Class B
m = 10
Subclassing an imported class
It is also possible to subclass a class that has been imported from
another package.
The output will be
Class B
m = 10
Class C
m = 10
n = 20
Access Protection
Adding a Class to a Package
It is simple to add a class to an existing package.
The package p1 contains one public class by name A. If we want to add another
class B to this package. This can be done as follows:
1. Define the class and make it public.
2. Place the package statement
package p1;
before the class definition as follows:
package p1;
public class B
{
//body of B
}
3. Store this as B.java file under the directory p1.
4. Compile B.java file. This will create a B.class file and place it in the
directory p1.
To create a package with multiple public classes, we may follow the following
steps:
1.Decide the name of the package
2. Create a subdirectory with this name under the directory where main
source files are stored.
3. Create classes that are to be placed in the package in separate source
files and declare the package statement
package packagename;
at the top of each source file
4. Switch to the subdirectory created earlier and compile each source file.
when completed, the package would contain .class files of all the source
files.

More Related Content

PDF
Triggers and active database
BalaMuruganSamuthira
 
PDF
Major and Minor Elements of Object Model
sohailsaif
 
PPTX
Er model
Soumyajit Dutta
 
PPTX
INTER PROCESS COMMUNICATION (IPC).pptx
LECO9
 
PPTX
Java.util
Ramakrishna kapa
 
PPT
Chapter 4 a interprocess communication
AbDul ThaYyal
 
PPTX
Attribute grammer
ahmed51236
 
DOCX
Operating System Process Synchronization
Haziq Naeem
 
Triggers and active database
BalaMuruganSamuthira
 
Major and Minor Elements of Object Model
sohailsaif
 
Er model
Soumyajit Dutta
 
INTER PROCESS COMMUNICATION (IPC).pptx
LECO9
 
Java.util
Ramakrishna kapa
 
Chapter 4 a interprocess communication
AbDul ThaYyal
 
Attribute grammer
ahmed51236
 
Operating System Process Synchronization
Haziq Naeem
 

What's hot (20)

PPTX
Decomposition methods in DBMS
soniyagoyal3
 
PPTX
Message passing ( in computer science)
Computer_ at_home
 
PDF
The Object Model
yndaravind
 
PPT
12 process control blocks
myrajendra
 
PDF
DDBMS Paper with Solution
Gyanmanjari Institute Of Technology
 
PPT
Unit 1 Java
arnold 7490
 
PPT
Abstract class in java
Lovely Professional University
 
PPTX
Java package
CS_GDRCST
 
ODP
Multithreading In Java
parag
 
PPTX
Protection Structures & Capabilities in Operating System
Meghaj Mallick
 
PPT
Object and class relationships
Pooja mittal
 
PPT
Kernel mode vs user mode in linux
Siddique Ibrahim
 
PPTX
Process scheduling
V.V.Vanniaperumal College for Women
 
PPTX
Packages,static,this keyword in java
Vishnu Suresh
 
PPTX
Access to non local names
Varsha Kumar
 
PPTX
Introduction to oop and java fundamentals
AnsgarMary
 
PPTX
Distributed design alternatives
Pooja Dixit
 
PPTX
Multi threading
gndu
 
PPTX
Integrity Constraints
Megha yadav
 
PPTX
Encapsulation
saikat rahman
 
Decomposition methods in DBMS
soniyagoyal3
 
Message passing ( in computer science)
Computer_ at_home
 
The Object Model
yndaravind
 
12 process control blocks
myrajendra
 
DDBMS Paper with Solution
Gyanmanjari Institute Of Technology
 
Unit 1 Java
arnold 7490
 
Abstract class in java
Lovely Professional University
 
Java package
CS_GDRCST
 
Multithreading In Java
parag
 
Protection Structures & Capabilities in Operating System
Meghaj Mallick
 
Object and class relationships
Pooja mittal
 
Kernel mode vs user mode in linux
Siddique Ibrahim
 
Packages,static,this keyword in java
Vishnu Suresh
 
Access to non local names
Varsha Kumar
 
Introduction to oop and java fundamentals
AnsgarMary
 
Distributed design alternatives
Pooja Dixit
 
Multi threading
gndu
 
Integrity Constraints
Megha yadav
 
Encapsulation
saikat rahman
 
Ad

Similar to Packages (20)

PPT
PACKAGE.PPT12345678912345949745654646455
meetjaju38
 
PPTX
java package in java.. in java packages.
ArunPatrickK1
 
PPTX
java package java package in java packages
ArunPatrick2
 
PPTX
javapackage,try,cthrow,finallytch,-160518085421 (1).pptx
ArunPatrick2
 
PPT
packages.ppt
SanthiNivas
 
PPTX
Package in Java
lalithambiga kamaraj
 
PPTX
Packages in java
SahithiReddyEtikala
 
PPTX
Packages in java
Elizabeth alexander
 
PPT
Packages,interfaces and exceptions
Mavoori Soshmitha
 
PPTX
Packages in java
Kavitha713564
 
DOCX
Module-4 Java Notes.docx notes about java
KaviShetty
 
PPTX
Java packages oop
Kawsar Hamid Sumon
 
PPTX
Java packags
Padma Kannan
 
PPTX
Introduction to package in java
Prognoz Technologies Pvt. Ltd.
 
PPTX
packages in java & c++
pankaj chelak
 
PPTX
THE PACKAGES CONCEPT IN JAVA PROGRAMMING.pptx
Kavitha713564
 
PPTX
Packages
Monika Mishra
 
PPT
7.Packages and Interfaces(MB).ppt .
happycocoman
 
DOCX
Practice Program-9-Packages-Unit 4.docx
R.K.College of engg & Tech
 
PPTX
Lecture 9 access modifiers and packages
manish kumar
 
PACKAGE.PPT12345678912345949745654646455
meetjaju38
 
java package in java.. in java packages.
ArunPatrickK1
 
java package java package in java packages
ArunPatrick2
 
javapackage,try,cthrow,finallytch,-160518085421 (1).pptx
ArunPatrick2
 
packages.ppt
SanthiNivas
 
Package in Java
lalithambiga kamaraj
 
Packages in java
SahithiReddyEtikala
 
Packages in java
Elizabeth alexander
 
Packages,interfaces and exceptions
Mavoori Soshmitha
 
Packages in java
Kavitha713564
 
Module-4 Java Notes.docx notes about java
KaviShetty
 
Java packages oop
Kawsar Hamid Sumon
 
Java packags
Padma Kannan
 
Introduction to package in java
Prognoz Technologies Pvt. Ltd.
 
packages in java & c++
pankaj chelak
 
THE PACKAGES CONCEPT IN JAVA PROGRAMMING.pptx
Kavitha713564
 
Packages
Monika Mishra
 
7.Packages and Interfaces(MB).ppt .
happycocoman
 
Practice Program-9-Packages-Unit 4.docx
R.K.College of engg & Tech
 
Lecture 9 access modifiers and packages
manish kumar
 
Ad

Recently uploaded (20)

PDF
Download iTop VPN Free 6.1.0.5882 Crack Full Activated Pre Latest 2025
imang66g
 
PPTX
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
PDF
Generating Union types w/ Static Analysis
K. Matthew Dupree
 
PPTX
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
PDF
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
PDF
Enhancing Healthcare RPM Platforms with Contextual AI Integration
Cadabra Studio
 
PDF
Protecting the Digital World Cyber Securit
dnthakkar16
 
PPTX
Explanation about Structures in C language.pptx
Veeral Rathod
 
PPTX
Role Of Python In Programing Language.pptx
jaykoshti048
 
PDF
Applitools Platform Pulse: What's New and What's Coming - July 2025
Applitools
 
PPTX
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
PDF
What to consider before purchasing Microsoft 365 Business Premium_PDF.pdf
Q-Advise
 
PPTX
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
Appium Automation Testing Tutorial PDF: Learn Mobile Testing in 7 Days
jamescantor38
 
PPTX
Presentation about Database and Database Administrator
abhishekchauhan86963
 
PPTX
PFAS Reporting Requirements 2026 Are You Submission Ready Certivo.pptx
Certivo Inc
 
DOCX
Can You Build Dashboards Using Open Source Visualization Tool.docx
Varsha Nayak
 
PPTX
ASSIGNMENT_1[1][1][1][1][1] (1) variables.pptx
kr2589474
 
PPTX
Presentation about variables and constant.pptx
kr2589474
 
PPTX
Contractor Management Platform and Software Solution for Compliance
SHEQ Network Limited
 
Download iTop VPN Free 6.1.0.5882 Crack Full Activated Pre Latest 2025
imang66g
 
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
Generating Union types w/ Static Analysis
K. Matthew Dupree
 
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
Enhancing Healthcare RPM Platforms with Contextual AI Integration
Cadabra Studio
 
Protecting the Digital World Cyber Securit
dnthakkar16
 
Explanation about Structures in C language.pptx
Veeral Rathod
 
Role Of Python In Programing Language.pptx
jaykoshti048
 
Applitools Platform Pulse: What's New and What's Coming - July 2025
Applitools
 
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
What to consider before purchasing Microsoft 365 Business Premium_PDF.pdf
Q-Advise
 
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Appium Automation Testing Tutorial PDF: Learn Mobile Testing in 7 Days
jamescantor38
 
Presentation about Database and Database Administrator
abhishekchauhan86963
 
PFAS Reporting Requirements 2026 Are You Submission Ready Certivo.pptx
Certivo Inc
 
Can You Build Dashboards Using Open Source Visualization Tool.docx
Varsha Nayak
 
ASSIGNMENT_1[1][1][1][1][1] (1) variables.pptx
kr2589474
 
Presentation about variables and constant.pptx
kr2589474
 
Contractor Management Platform and Software Solution for Compliance
SHEQ Network Limited
 

Packages

  • 2. Introduction ❖ The main features of OOP is its ability to reuse the code already created, ➢ One way of achieving this is by the use of packages ❖ Packages are Java’s way of grouping a variety of classes or interfaces together. ❖ Packages act as “containers” for classes.
  • 3. Benefits ❖ The classes contained in the packages of other programs can be reused. ❖ In packages, classes can be unique compared with classes in other packages. ❖ Packages provides a way to hide classes.
  • 4. Java API Packages ❖ Java API provides a large number of classes grouped into different packages according to functionality ❖ Most of the time we use the packages available with the Java API.
  • 6. Java System Packages and Their Classes
  • 7. Using System Packages Hierarchical representation of java.awt package
  • 8. ❖ The packages are organised in a hierarchical structure. ❖ It show package name java contains the package awt, which in turn contains various classes required for implementing graphical user interface. ❖ There are two ways of accessing the classes stored in a package. ■ Fully qualified class name ■ Import statements
  • 9. ❖ The first approach is to use the fully qualified class name of the class we want to use. java.awt.Colour ❖ Awt is a package within the package java and the hierarchy is represented by separating the levels with dots. Import packagename.classname; Or Import packagename.*; ❖ These are known as import statements and must appear at the top of the file before any class declarations.
  • 10. Import java.awt.Color; ❖ This statement allows the specified class in the specified package to be imported. ❖ The above statement imports the class Colour and therefore the class name can now be directly used in the program. Import java.awt.*; ❖ This statement imports every class contained in the specified package. ❖ It will bring all classes of java.awt package.
  • 11. Naming Conventions ❖ Packages can be named using the standard Java naming rules. ❖ Packages should begin with lowercase letters by convention. ❖ It distinguish package names from class names.
  • 12. Creating Packages ❖ We must first declare the name of the package using the package keyword followed by a package name. ❖ This must be the first statement in a Java source file. Example
  • 13. Accessing a Package ❖ Java system package can be accessed ➢ Using a fully qualified class name. Or ➢ Using a shortcut approach through the import statement. ❖ The same approaches can be used to access the user defined packages as well.
  • 14. ❖ The general form of import statement for searching a class is as follows: ❖ Package 1 is the name of the top level package, package2 is the name of the package that is inside the package1, and so on. Finally, the explicit classname is specified. ❖ Another approach as follows: ■ Import packagename.*; ❖ The star(*) indicates that the compiler should search entire package hierarchy when it encounters a class name.
  • 15. Using a Package It shows a package named package1 containing a single ClassA
  • 16. This program that imports the class ClassA from the package package1.
  • 17. Let us consider another package named package2 containing again a single class.
  • 18. In this program, it uses classes contained in both the packages. So it imports package1 and package2.
  • 19. The output will be Class A Class B m = 10
  • 21. It is also possible to subclass a class that has been imported from another package. The output will be Class B m = 10 Class C m = 10 n = 20
  • 23. Adding a Class to a Package It is simple to add a class to an existing package.
  • 24. The package p1 contains one public class by name A. If we want to add another class B to this package. This can be done as follows: 1. Define the class and make it public. 2. Place the package statement package p1; before the class definition as follows: package p1; public class B { //body of B }
  • 25. 3. Store this as B.java file under the directory p1. 4. Compile B.java file. This will create a B.class file and place it in the directory p1. To create a package with multiple public classes, we may follow the following steps: 1.Decide the name of the package 2. Create a subdirectory with this name under the directory where main source files are stored.
  • 26. 3. Create classes that are to be placed in the package in separate source files and declare the package statement package packagename; at the top of each source file 4. Switch to the subdirectory created earlier and compile each source file. when completed, the package would contain .class files of all the source files.