SlideShare a Scribd company logo
1 of 10
Object Oriented Programming with
JAVA
Topic Name : Java Package
Package
Use of
package
Class path
Import
statement
Access
Control
1). Use of Package
• Packages in Java is a mechanism to encapsulate a
group of classes, interfaces and sub packages.
• Many implementations of Java use a hierarchical file
system to manage source and class files.
• It is easy to organize class files into packages.
• All we need to do is put related class files in the same
directory, give the directory a name that relates to the
purpose of the classes, and add a line to the top of
each class file that declares the package name, which
is the same as the directory name where they reside.
1). Use of package in Java
• In java there are already many predefined
packages that we use while programming.
• For example: java.lang, java.io, java.util etc.
However one of the most useful feature of java is
that we can define our own packages
Advantages of Package
• Reusability: Reusability of code is one of the most
important requirements in the software industry.
Reusability saves time, effort and also ensures
consistency. A class once developed can be reused by
any number of programs wishing to incorporate the
class in that particular program.
• Easy to locate the files.
• In real life situation there may arise scenarios where
we need to define files of the same name. This may
lead to “name-space collisions”. Packages are a way of
avoiding “name-space collisions”.
How to Use Package
package tools;
public class Hammer {
public void id () {
System.out.println ("Hammer");
}
}
1. At most one package declaration can appear in a source file.
2. The package declaration must be the first statement in the unit.
2. IMPORT keyword Use/Description
We can call it by its full name. For instance,
com.myPackage1.myPackage2 myNewClass = new com.myPackage1.myPackage2();
However this method is very time consuming. So
normally we use the second method.
2. We use the “import” keyword to access packages.
If you say import com.myPackage1.myPackage2,
then every time we type “myPackage2”, the
compiler will understand that we mean
com.myPackage1.myPackage2. So we can do..
Classpath
It is a environmental variable, which contains the
path for the default-working directory (.).
The specific location that java compiler will
consider, as the root of any package hierarchy is,
controlled by Classpath
import com.myPackage1.myPackage2;
class myClass { myPackage2 myNewClass= new
myPackage2 ();
…
…
… }
Access Specifier
• private: accessible only in the class
• no modifier: so-called “package” access —
accessible only in the same package
• protected: accessible (inherited) by subclasses,
and accessible by code in same package
• public: accessible anywhere the class is accessible,
and inherited by subclasses

More Related Content

What's hot

What's hot (19)

Spring 2.0 技術手冊第三章 - IoC 容器
Spring 2.0 技術手冊第三章 - IoC 容器Spring 2.0 技術手冊第三章 - IoC 容器
Spring 2.0 技術手冊第三章 - IoC 容器
 
JDK versions and OpenJDK
JDK versions and OpenJDKJDK versions and OpenJDK
JDK versions and OpenJDK
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Packages in java
Packages in javaPackages in java
Packages in java
 
computer science JAVA ppt
computer science JAVA pptcomputer science JAVA ppt
computer science JAVA ppt
 
Java calendar
Java calendarJava calendar
Java calendar
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in java
 
Java Applet and Graphics
Java Applet and GraphicsJava Applet and Graphics
Java Applet and Graphics
 
Applicazioni web based
Applicazioni web basedApplicazioni web based
Applicazioni web based
 
Les collections en Java
Les collections en JavaLes collections en Java
Les collections en Java
 
Introduction to package in java
Introduction to package in javaIntroduction to package in java
Introduction to package in java
 
Java string handling
Java string handlingJava string handling
Java string handling
 
Gradle - the Enterprise Automation Tool
Gradle  - the Enterprise Automation ToolGradle  - the Enterprise Automation Tool
Gradle - the Enterprise Automation Tool
 
Arrays in java language
Arrays in java languageArrays in java language
Arrays in java language
 
Introduction to Ruby on Rails
Introduction to Ruby on RailsIntroduction to Ruby on Rails
Introduction to Ruby on Rails
 
Java packages
Java packagesJava packages
Java packages
 
java ppt.pdf
java ppt.pdfjava ppt.pdf
java ppt.pdf
 
Struts,Jsp,Servlet
Struts,Jsp,ServletStruts,Jsp,Servlet
Struts,Jsp,Servlet
 
Introduction of java
Introduction  of javaIntroduction  of java
Introduction of java
 

Similar to Package In Java

Similar to Package In Java (20)

Java package
Java packageJava package
Java package
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Packages
PackagesPackages
Packages
 
Class notes(week 7) on packages
Class notes(week 7) on packagesClass notes(week 7) on packages
Class notes(week 7) on packages
 
Java - Packages Concepts
Java - Packages ConceptsJava - Packages Concepts
Java - Packages Concepts
 
Class notes(week 7) on packages
Class notes(week 7) on packagesClass notes(week 7) on packages
Class notes(week 7) on packages
 
Java packags
Java packagsJava packags
Java packags
 
Unit 4 Java
Unit 4 JavaUnit 4 Java
Unit 4 Java
 
Java packages
Java packagesJava packages
Java packages
 
Package in Java
Package in JavaPackage in Java
Package in Java
 
Java packages oop
Java packages oopJava packages oop
Java packages oop
 
Unit 2 notes.pdf
Unit 2 notes.pdfUnit 2 notes.pdf
Unit 2 notes.pdf
 
Z blue interfaces and packages (37129912)
Z blue   interfaces and  packages (37129912)Z blue   interfaces and  packages (37129912)
Z blue interfaces and packages (37129912)
 
Lecture 19
Lecture 19Lecture 19
Lecture 19
 
Package.pptx
Package.pptxPackage.pptx
Package.pptx
 
Unit4 java
Unit4 javaUnit4 java
Unit4 java
 
Packages access protection, importing packages
Packages   access protection, importing packagesPackages   access protection, importing packages
Packages access protection, importing packages
 
Java basics
Java basicsJava basics
Java basics
 
packages in java & c++
packages in java & c++packages in java & c++
packages in java & c++
 

Recently uploaded

"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesZilliz
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 

Recently uploaded (20)

"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector Databases
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 

Package In Java

  • 1. Object Oriented Programming with JAVA Topic Name : Java Package
  • 3. 1). Use of Package • Packages in Java is a mechanism to encapsulate a group of classes, interfaces and sub packages. • Many implementations of Java use a hierarchical file system to manage source and class files. • It is easy to organize class files into packages. • All we need to do is put related class files in the same directory, give the directory a name that relates to the purpose of the classes, and add a line to the top of each class file that declares the package name, which is the same as the directory name where they reside.
  • 4. 1). Use of package in Java • In java there are already many predefined packages that we use while programming. • For example: java.lang, java.io, java.util etc. However one of the most useful feature of java is that we can define our own packages
  • 5. Advantages of Package • Reusability: Reusability of code is one of the most important requirements in the software industry. Reusability saves time, effort and also ensures consistency. A class once developed can be reused by any number of programs wishing to incorporate the class in that particular program. • Easy to locate the files. • In real life situation there may arise scenarios where we need to define files of the same name. This may lead to “name-space collisions”. Packages are a way of avoiding “name-space collisions”.
  • 6. How to Use Package package tools; public class Hammer { public void id () { System.out.println ("Hammer"); } } 1. At most one package declaration can appear in a source file. 2. The package declaration must be the first statement in the unit.
  • 7. 2. IMPORT keyword Use/Description We can call it by its full name. For instance, com.myPackage1.myPackage2 myNewClass = new com.myPackage1.myPackage2(); However this method is very time consuming. So normally we use the second method. 2. We use the “import” keyword to access packages. If you say import com.myPackage1.myPackage2, then every time we type “myPackage2”, the compiler will understand that we mean com.myPackage1.myPackage2. So we can do..
  • 8. Classpath It is a environmental variable, which contains the path for the default-working directory (.). The specific location that java compiler will consider, as the root of any package hierarchy is, controlled by Classpath
  • 9. import com.myPackage1.myPackage2; class myClass { myPackage2 myNewClass= new myPackage2 (); … … … }
  • 10. Access Specifier • private: accessible only in the class • no modifier: so-called “package” access — accessible only in the same package • protected: accessible (inherited) by subclasses, and accessible by code in same package • public: accessible anywhere the class is accessible, and inherited by subclasses