SlideShare une entreprise Scribd logo
1  sur  7
PACKAGES
It’s a group of related classes and interfaces; it helps to organize your code and provide
another layer of encapsulation and also helps to provide another level of control over the java
code.
Packages serve two purposes
 It provides mechanism by which related pieces of a program can be organized as a
unit.
Classes defined within a package must be accessed through their package name
Where packages provide a way to name collection of classes.
 Second, a package participates in java’s access control mechanism.
Classes defined within package can be made private to that package & not
Accessible outside code.
When a class is defined within package, the name of that package is attached to the class
which will further helpful for name collision
Defining a Package:
To create a Package need to use the Package Statement located at the top of the source file.
And a class declared within that file will belong to specified package.
Syntax
package packageName;
Eg: package pkg;
Here name of the package is pkg
 More than one file can include same package statement
 You can create hierarchy of Packages and they are separated by period
Eg: package pack1.pack2.pack3.........packN;
 One can easily Create directories
 Eg: package alpha.beta.gamma;
Stored in ..../alpha/beta/gamma.....
Example Code:
package myPackage;
public class DefiningPackage {
public static void main(String[] args) {
System.out.println("This class belongs to myPackage.");
}
}
Output: This class belongs to myPackage
Example2:
package mypack;
public class Simple{
public static void main(String args[]){
System.out.println("Welcome to package");
}
}
Output: Welcome to package
Package in java can be categorized in two form, built-in package and user-defined package.
There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc.
Simple Code for Java Package
To Execute the Bellow Code need to go for  File then New Project Provide the
Program Name the Go For SRC (Source Code)  Then Main
Next Right Click on the Main  Next Click on New Then Select Package Shown Bellow
Then one more window will pop up then Need to Provide the Name for the Package you want
to create for Example “mypack” is the name of the Package
Now mypack Pakage is Created Shown bellow
Next if you want to create class under your package then you have to click on the main option
provided bellow of your package created  Next you have to right click on main option
Next you have to create a class by providing name of the class you want to create
Now you can see that you have created your own Package named as mypack.A
1. //save by A.java
2. package mypack;
3. public class A{
4. public void msg(){System.out.println("Hello");}
5. }
6. save by A.java
1. //save by B.java
2. package mypack;
save by B.java
3. import pack.*;
4.
5. class B{
6. public static void main(String args[]){
7. A obj = new A();
8. obj.msg();
9. }
}
Output: Hello When you Run A.java

Contenu connexe

Similaire à Practice Program-9-Packages-Unit 4.docx

Similaire à Practice Program-9-Packages-Unit 4.docx (20)

Java packages oop
Java packages oopJava packages oop
Java packages oop
 
Packages
PackagesPackages
Packages
 
Java Packages
Java Packages Java Packages
Java Packages
 
Packages access protection, importing packages
Packages   access protection, importing packagesPackages   access protection, importing packages
Packages access protection, importing packages
 
Java packags
Java packagsJava packags
Java packags
 
Java - Packages Concepts
Java - Packages ConceptsJava - Packages Concepts
Java - Packages Concepts
 
Java packages
Java packagesJava packages
Java packages
 
Packages,interfaces and exceptions
Packages,interfaces and exceptionsPackages,interfaces and exceptions
Packages,interfaces and exceptions
 
Class notes(week 7) on packages
Class notes(week 7) on packagesClass notes(week 7) on packages
Class notes(week 7) on 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 .
 
packages unit 5 .ppt
packages  unit 5 .pptpackages  unit 5 .ppt
packages unit 5 .ppt
 
packages.ppt
packages.pptpackages.ppt
packages.ppt
 
packages.ppt
packages.pptpackages.ppt
packages.ppt
 
Lecture 19
Lecture 19Lecture 19
Lecture 19
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in java
 
Java packages
Java packagesJava packages
Java packages
 
Java package
Java packageJava package
Java package
 
Packages and interface
Packages and interfacePackages and interface
Packages and interface
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Packages in java
Packages in javaPackages in java
Packages in java
 

Plus de R.K.College of engg & Tech (15)

Module 5(Matplotlib and tkinter).pdf
Module 5(Matplotlib and tkinter).pdfModule 5(Matplotlib and tkinter).pdf
Module 5(Matplotlib and tkinter).pdf
 
Module 5(Numpy).pdf
Module 5(Numpy).pdfModule 5(Numpy).pdf
Module 5(Numpy).pdf
 
Module 5(Pandas).pdf
Module 5(Pandas).pdfModule 5(Pandas).pdf
Module 5(Pandas).pdf
 
Module IV_updated(old).pdf
Module IV_updated(old).pdfModule IV_updated(old).pdf
Module IV_updated(old).pdf
 
Module III.pdf
Module III.pdfModule III.pdf
Module III.pdf
 
Python_Module_2.pdf
Python_Module_2.pdfPython_Module_2.pdf
Python_Module_2.pdf
 
Python_Module_1.pdf
Python_Module_1.pdfPython_Module_1.pdf
Python_Module_1.pdf
 
Python for Data Analysis_ Data Wrangling with Pandas, Numpy, and Ipython ( PD...
Python for Data Analysis_ Data Wrangling with Pandas, Numpy, and Ipython ( PD...Python for Data Analysis_ Data Wrangling with Pandas, Numpy, and Ipython ( PD...
Python for Data Analysis_ Data Wrangling with Pandas, Numpy, and Ipython ( PD...
 
Ch-8.pdf
Ch-8.pdfCh-8.pdf
Ch-8.pdf
 
Ch-5.pdf
Ch-5.pdfCh-5.pdf
Ch-5.pdf
 
Ch-5.pdf
Ch-5.pdfCh-5.pdf
Ch-5.pdf
 
Ch-4.pdf
Ch-4.pdfCh-4.pdf
Ch-4.pdf
 
Ch-3.pdf
Ch-3.pdfCh-3.pdf
Ch-3.pdf
 
Creating Interface- Practice Program 6.docx
Creating Interface- Practice Program 6.docxCreating Interface- Practice Program 6.docx
Creating Interface- Practice Program 6.docx
 
Unit-3 Practice Programs-5.docx
Unit-3 Practice Programs-5.docxUnit-3 Practice Programs-5.docx
Unit-3 Practice Programs-5.docx
 

Dernier

result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
Tonystark477637
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Christo Ananth
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Dernier (20)

result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICSUNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spain
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 

Practice Program-9-Packages-Unit 4.docx

  • 1. PACKAGES It’s a group of related classes and interfaces; it helps to organize your code and provide another layer of encapsulation and also helps to provide another level of control over the java code. Packages serve two purposes  It provides mechanism by which related pieces of a program can be organized as a unit. Classes defined within a package must be accessed through their package name Where packages provide a way to name collection of classes.  Second, a package participates in java’s access control mechanism. Classes defined within package can be made private to that package & not Accessible outside code. When a class is defined within package, the name of that package is attached to the class which will further helpful for name collision Defining a Package: To create a Package need to use the Package Statement located at the top of the source file. And a class declared within that file will belong to specified package. Syntax package packageName; Eg: package pkg; Here name of the package is pkg  More than one file can include same package statement  You can create hierarchy of Packages and they are separated by period Eg: package pack1.pack2.pack3.........packN;  One can easily Create directories  Eg: package alpha.beta.gamma; Stored in ..../alpha/beta/gamma.....
  • 2. Example Code: package myPackage; public class DefiningPackage { public static void main(String[] args) { System.out.println("This class belongs to myPackage."); } } Output: This class belongs to myPackage Example2: package mypack; public class Simple{ public static void main(String args[]){ System.out.println("Welcome to package"); } } Output: Welcome to package Package in java can be categorized in two form, built-in package and user-defined package. There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc.
  • 3. Simple Code for Java Package To Execute the Bellow Code need to go for  File then New Project Provide the Program Name the Go For SRC (Source Code)  Then Main Next Right Click on the Main  Next Click on New Then Select Package Shown Bellow Then one more window will pop up then Need to Provide the Name for the Package you want to create for Example “mypack” is the name of the Package
  • 4. Now mypack Pakage is Created Shown bellow Next if you want to create class under your package then you have to click on the main option provided bellow of your package created  Next you have to right click on main option
  • 5. Next you have to create a class by providing name of the class you want to create Now you can see that you have created your own Package named as mypack.A
  • 6. 1. //save by A.java 2. package mypack; 3. public class A{ 4. public void msg(){System.out.println("Hello");} 5. } 6. save by A.java 1. //save by B.java 2. package mypack; save by B.java
  • 7. 3. import pack.*; 4. 5. class B{ 6. public static void main(String args[]){ 7. A obj = new A(); 8. obj.msg(); 9. } } Output: Hello When you Run A.java