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

Similar to Packages

Java packags
Java packagsJava packags
Java packags
Padma Kannan
 
Introduction to package in java
Introduction to package in javaIntroduction to package in java
Introduction to package in java
Prognoz Technologies Pvt. Ltd.
 
packages in java & c++
packages in java & c++packages in java & c++
packages in java & c++
pankaj chelak
 
THE PACKAGES CONCEPT IN JAVA PROGRAMMING.pptx
THE PACKAGES CONCEPT  IN JAVA PROGRAMMING.pptxTHE PACKAGES CONCEPT  IN JAVA PROGRAMMING.pptx
THE PACKAGES CONCEPT IN JAVA PROGRAMMING.pptx
Kavitha713564
 
Packages
PackagesPackages
Packages
Monika Mishra
 
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
 
Practice Program-9-Packages-Unit 4.docx
Practice Program-9-Packages-Unit 4.docxPractice Program-9-Packages-Unit 4.docx
Practice Program-9-Packages-Unit 4.docx
R.K.College of engg & Tech
 
Lecture 9 access modifiers and packages
Lecture   9 access modifiers and packagesLecture   9 access modifiers and packages
Lecture 9 access modifiers and packages
manish kumar
 
Packages
PackagesPackages
Packages
Nuha Noor
 
Java - Packages Concepts
Java - Packages ConceptsJava - Packages Concepts
Java - Packages Concepts
Victer Paul
 
OOP_packages_222902019.pptx
OOP_packages_222902019.pptxOOP_packages_222902019.pptx
OOP_packages_222902019.pptx
222902019
 
OOP_packages_222902019.pptx
OOP_packages_222902019.pptxOOP_packages_222902019.pptx
OOP_packages_222902019.pptx
222902019
 
Class notes(week 7) on packages
Class notes(week 7) on packagesClass notes(week 7) on packages
Class notes(week 7) on packages
Kuntal Bhowmick
 
Lecture-12 Java Packages and GUI Basics.ppt
Lecture-12 Java Packages and GUI Basics.pptLecture-12 Java Packages and GUI Basics.ppt
Lecture-12 Java Packages and GUI Basics.ppt
lirogal422
 
Unit4 java
Unit4 javaUnit4 java
Unit4 javamrecedu
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in java
Vishnu Suresh
 
packages unit 5 .ppt
packages  unit 5 .pptpackages  unit 5 .ppt
packages unit 5 .ppt
thenmozhip8
 
packages.ppt
packages.pptpackages.ppt
packages.ppt
radhika477746
 

Similar to Packages (20)

Java packags
Java packagsJava packags
Java packags
 
Introduction to package in java
Introduction to package in javaIntroduction to package in java
Introduction to package in java
 
packages in java & c++
packages in java & c++packages in java & c++
packages in java & c++
 
THE PACKAGES CONCEPT IN JAVA PROGRAMMING.pptx
THE PACKAGES CONCEPT  IN JAVA PROGRAMMING.pptxTHE PACKAGES CONCEPT  IN JAVA PROGRAMMING.pptx
THE PACKAGES CONCEPT IN JAVA PROGRAMMING.pptx
 
Packages
PackagesPackages
Packages
 
7.Packages and Interfaces(MB).ppt .
7.Packages and Interfaces(MB).ppt             .7.Packages and Interfaces(MB).ppt             .
7.Packages and Interfaces(MB).ppt .
 
Practice Program-9-Packages-Unit 4.docx
Practice Program-9-Packages-Unit 4.docxPractice Program-9-Packages-Unit 4.docx
Practice Program-9-Packages-Unit 4.docx
 
Lecture 9 access modifiers and packages
Lecture   9 access modifiers and packagesLecture   9 access modifiers and packages
Lecture 9 access modifiers and packages
 
Packages
PackagesPackages
Packages
 
Java - Packages Concepts
Java - Packages ConceptsJava - Packages Concepts
Java - Packages Concepts
 
Unit 2 notes.pdf
Unit 2 notes.pdfUnit 2 notes.pdf
Unit 2 notes.pdf
 
OOP_packages_222902019.pptx
OOP_packages_222902019.pptxOOP_packages_222902019.pptx
OOP_packages_222902019.pptx
 
OOP_packages_222902019.pptx
OOP_packages_222902019.pptxOOP_packages_222902019.pptx
OOP_packages_222902019.pptx
 
Class notes(week 7) on packages
Class notes(week 7) on packagesClass notes(week 7) on packages
Class notes(week 7) on packages
 
Lecture-12 Java Packages and GUI Basics.ppt
Lecture-12 Java Packages and GUI Basics.pptLecture-12 Java Packages and GUI Basics.ppt
Lecture-12 Java Packages and GUI Basics.ppt
 
Unit 4 Java
Unit 4 JavaUnit 4 Java
Unit 4 Java
 
Unit4 java
Unit4 javaUnit4 java
Unit4 java
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in java
 
packages unit 5 .ppt
packages  unit 5 .pptpackages  unit 5 .ppt
packages unit 5 .ppt
 
packages.ppt
packages.pptpackages.ppt
packages.ppt
 

Recently uploaded

What is Augmented Reality Image Tracking
What is Augmented Reality Image TrackingWhat is Augmented Reality Image Tracking
What is Augmented Reality Image Tracking
pavan998932
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke
 
Mobile app Development Services | Drona Infotech
Mobile app Development Services  | Drona InfotechMobile app Development Services  | Drona Infotech
Mobile app Development Services | Drona Infotech
Drona Infotech
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j
 
E-commerce Application Development Company.pdf
E-commerce Application Development Company.pdfE-commerce Application Development Company.pdf
E-commerce Application Development Company.pdf
Hornet Dynamics
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
Google
 
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
Alina Yurenko
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
timtebeek1
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
Neo4j
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
Shane Coughlan
 
Empowering Growth with Best Software Development Company in Noida - Deuglo
Empowering Growth with Best Software  Development Company in Noida - DeugloEmpowering Growth with Best Software  Development Company in Noida - Deuglo
Empowering Growth with Best Software Development Company in Noida - Deuglo
Deuglo Infosystem Pvt Ltd
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
Adele Miller
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
NYGGS Automation Suite
 
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket ManagementUtilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate
 
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Crescat
 
Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
Rakesh Kumar R
 
SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024
Hironori Washizaki
 
在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
mz5nrf0n
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
TheSMSPoint
 

Recently uploaded (20)

What is Augmented Reality Image Tracking
What is Augmented Reality Image TrackingWhat is Augmented Reality Image Tracking
What is Augmented Reality Image Tracking
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
 
Mobile app Development Services | Drona Infotech
Mobile app Development Services  | Drona InfotechMobile app Development Services  | Drona Infotech
Mobile app Development Services | Drona Infotech
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
 
E-commerce Application Development Company.pdf
E-commerce Application Development Company.pdfE-commerce Application Development Company.pdf
E-commerce Application Development Company.pdf
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
 
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
 
Empowering Growth with Best Software Development Company in Noida - Deuglo
Empowering Growth with Best Software  Development Company in Noida - DeugloEmpowering Growth with Best Software  Development Company in Noida - Deuglo
Empowering Growth with Best Software Development Company in Noida - Deuglo
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
 
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket ManagementUtilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
 
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
 
Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
 
SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024
 
在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
 

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.