SlideShare a Scribd company logo
1 of 18
PACKAGES IN JAVA
Mrs.N.Kavitha
Head,Department of Computer Science,
E.M.G.Yadava Women’s College
Madurai-14.
PACKAGES IN JAVA
A java package is a group of similar types of classes, interfaces and sub-packages.
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.
Advantage of Java Package:
 Java package is used to categorize the classes and interfaces so that they can be easily
maintained.
 Java package provides access protection.
Java package removes naming collision.
Simple example of java package:
The package keyword is used to create a package in java.
//save as Simple.java
package mypack;
public class Simple
{
public static void main(String args[])
{
System.out.println("Welcome to package");
}
}
• How to access package from another package?
• There are three ways to access the package from outside
the package.
1.import package.*;
2.import package.classname;
3.fully qualified name.
• 1) Using packagename.*
• If you use package.* then all the classes and interfaces
of this package will be accessible but not subpackages.
• The import keyword is used to make the classes and
interface of another package accessible to the current
package.
Example of package that import the packagename.*
//save by A.java
package pack;
public class A
{
public void msg()
{
System.out.println("Hello");
}
}
//save by B.java
package mypack;
import pack.*;
class B
{
public static void main(String args[])
{
A obj = new A();
obj.msg();
}
}
2) Using packagename.classname:
If you import package.classname then only declared class of this
package will be accessible.
Example of package by import package.classname
//save by A.java
package pack;
public class A
{
public void msg()
{
System.out.println("Hello");
}
}
//save by B.java
package mypack;
import pack.A;
class B
{
public static void main(String args[])
{
A obj = new A();
obj.msg();
}
}
• 3) Using fully qualified name
• If you use fully qualified name then only declared class of this package will be
accessible. Now there is no need to import. But you need to use fully qualified
name every time when you are accessing the class or interface.
• It is generally used when two packages have same class name e.g. java.util
and java.sql packages contain Date class.
• Example of package by import fully qualified name
1.//save by A.java
2.package pack;
3.public class A{
4. public void msg(){System.out.println("Hello");}
5.}
6.//save by B.java
7.package mypack;
8.class B{
9. public static void main(String args[]){
10. pack.A obj = new pack.A();//using fully qualified name
11. obj.msg();
12. }
13.}
• Note: Sequence of the program must be package then import
then class.
Subpackage in java:
Package inside the package is called the subpackage. It should be
created to categorize the package further.
Sun Microsystem has definded a package named java that contains
many classes like System, String, Reader, Writer, Socket etc.
These classes represent a particular group e.g. Reader and Writer
classes are for Input/Output operation, Socket and ServerSocket
classes are for networking etc and so on.
So, Sun Microsoft has subcategorized the java package into
subpackages such as lang, net, io etc.
The Input/Output related classes in io package.
Server and ServerSocket classes in net packages and so on.
Example of Subpackage:
package com.javatpoint.core;
class Simple
{
public static void main(String args[])
{
System.out.println("Hello subpackage");
}
}
To Compile: javac -d . Simple.java
To Run: java com.javatpoint.core.Simple
Output:Hello subpackage
How to send the class file to another directory or drive?
The class file of A.java source file in classes folder of c: drive.
For example:
//save as Simple.java
package mypack;
public class Simple
{
public static void main(String args[])
{
System.out.println("Welcome to package");
}
}
To run this program from e:source directory, you need to set
classpath of the directory where the class file resides.
e:sources> set classpath=c:classes;.;
e:sources> java mypack.Simple
To Compile:
e:sources> javac -d c:classes Simple.java
To Run:
• Another way to run this program by -classpath switch of java:
• The -classpath switch can be used with javac and java tool.
• To run this program from e:source directory, you can use -
classpath switch of java that tells where to look for class file.
For example:
• e:sources> java -classpath c:classes mypack.Simple
Output:Welcome to package
There are two ways to load the class
files temporary and permanent.
Ways to load the class files or jar files
•Temporary
•By setting the classpath in the command prompt
•By -classpath switch
•Permanent
•By setting the classpath in the environment variables
•By creating the jar file, that contains all the class
files, and copying the jar file in the jre/lib/ext folder.

More Related Content

Similar to THE PACKAGES CONCEPT IN JAVA PROGRAMMING.pptx

Similar to THE PACKAGES CONCEPT IN JAVA PROGRAMMING.pptx (20)

Packages
PackagesPackages
Packages
 
packages.ppt
packages.pptpackages.ppt
packages.ppt
 
Introduction to package in java
Introduction to package in javaIntroduction to package in java
Introduction to package in java
 
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
 
Java - Packages Concepts
Java - Packages ConceptsJava - Packages Concepts
Java - Packages Concepts
 
Java packages oop
Java packages oopJava packages oop
Java packages oop
 
Packages
PackagesPackages
Packages
 
Packages
PackagesPackages
Packages
 
Java package
Java packageJava package
Java package
 
Javapackages 4th semester
Javapackages 4th semesterJavapackages 4th semester
Javapackages 4th semester
 
Lecture 9 access modifiers and packages
Lecture   9 access modifiers and packagesLecture   9 access modifiers and packages
Lecture 9 access modifiers and packages
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in java
 
Java packags
Java packagsJava packags
Java packags
 
Z blue interfaces and packages (37129912)
Z blue   interfaces and  packages (37129912)Z blue   interfaces and  packages (37129912)
Z blue interfaces and packages (37129912)
 
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
 
Packages and interface
Packages and interfacePackages and interface
Packages and interface
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Package in Java
Package in JavaPackage in Java
Package in Java
 
packages in java & c++
packages in java & c++packages in java & c++
packages in java & c++
 

More from Kavitha713564

The Java Server Page in Java Concept.pptx
The Java Server Page in Java Concept.pptxThe Java Server Page in Java Concept.pptx
The Java Server Page in Java Concept.pptxKavitha713564
 
Programming in python in detail concept .pptx
Programming in python in detail concept .pptxProgramming in python in detail concept .pptx
Programming in python in detail concept .pptxKavitha713564
 
The Input Statement in Core Python .pptx
The Input Statement in Core Python .pptxThe Input Statement in Core Python .pptx
The Input Statement in Core Python .pptxKavitha713564
 
The Datatypes Concept in Core Python.pptx
The Datatypes Concept in Core Python.pptxThe Datatypes Concept in Core Python.pptx
The Datatypes Concept in Core Python.pptxKavitha713564
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javaKavitha713564
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaKavitha713564
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaKavitha713564
 
Input output files in java
Input output files in javaInput output files in java
Input output files in javaKavitha713564
 
Arrays,string and vector
Arrays,string and vectorArrays,string and vector
Arrays,string and vectorKavitha713564
 

More from Kavitha713564 (13)

The Java Server Page in Java Concept.pptx
The Java Server Page in Java Concept.pptxThe Java Server Page in Java Concept.pptx
The Java Server Page in Java Concept.pptx
 
Programming in python in detail concept .pptx
Programming in python in detail concept .pptxProgramming in python in detail concept .pptx
Programming in python in detail concept .pptx
 
The Input Statement in Core Python .pptx
The Input Statement in Core Python .pptxThe Input Statement in Core Python .pptx
The Input Statement in Core Python .pptx
 
The Datatypes Concept in Core Python.pptx
The Datatypes Concept in Core Python.pptxThe Datatypes Concept in Core Python.pptx
The Datatypes Concept in Core Python.pptx
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Methods in Java
Methods in JavaMethods in Java
Methods in Java
 
Applet in java new
Applet in java newApplet in java new
Applet in java new
 
Basic of java
Basic of javaBasic of java
Basic of java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Input output files in java
Input output files in javaInput output files in java
Input output files in java
 
Arrays,string and vector
Arrays,string and vectorArrays,string and vector
Arrays,string and vector
 

Recently uploaded

Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 

Recently uploaded (20)

Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 

THE PACKAGES CONCEPT IN JAVA PROGRAMMING.pptx

  • 1. PACKAGES IN JAVA Mrs.N.Kavitha Head,Department of Computer Science, E.M.G.Yadava Women’s College Madurai-14.
  • 2.
  • 3. PACKAGES IN JAVA A java package is a group of similar types of classes, interfaces and sub-packages. 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. Advantage of Java Package:  Java package is used to categorize the classes and interfaces so that they can be easily maintained.  Java package provides access protection. Java package removes naming collision.
  • 4.
  • 5. Simple example of java package: The package keyword is used to create a package in java. //save as Simple.java package mypack; public class Simple { public static void main(String args[]) { System.out.println("Welcome to package"); } }
  • 6. • How to access package from another package? • There are three ways to access the package from outside the package. 1.import package.*; 2.import package.classname; 3.fully qualified name. • 1) Using packagename.* • If you use package.* then all the classes and interfaces of this package will be accessible but not subpackages. • The import keyword is used to make the classes and interface of another package accessible to the current package.
  • 7. Example of package that import the packagename.* //save by A.java package pack; public class A { public void msg() { System.out.println("Hello"); } } //save by B.java package mypack; import pack.*; class B { public static void main(String args[]) { A obj = new A(); obj.msg(); } }
  • 8. 2) Using packagename.classname: If you import package.classname then only declared class of this package will be accessible. Example of package by import package.classname //save by A.java package pack; public class A { public void msg() { System.out.println("Hello"); } }
  • 9. //save by B.java package mypack; import pack.A; class B { public static void main(String args[]) { A obj = new A(); obj.msg(); } }
  • 10. • 3) Using fully qualified name • If you use fully qualified name then only declared class of this package will be accessible. Now there is no need to import. But you need to use fully qualified name every time when you are accessing the class or interface. • It is generally used when two packages have same class name e.g. java.util and java.sql packages contain Date class. • Example of package by import fully qualified name 1.//save by A.java 2.package pack; 3.public class A{ 4. public void msg(){System.out.println("Hello");} 5.} 6.//save by B.java 7.package mypack; 8.class B{ 9. public static void main(String args[]){ 10. pack.A obj = new pack.A();//using fully qualified name 11. obj.msg(); 12. } 13.}
  • 11. • Note: Sequence of the program must be package then import then class.
  • 12. Subpackage in java: Package inside the package is called the subpackage. It should be created to categorize the package further. Sun Microsystem has definded a package named java that contains many classes like System, String, Reader, Writer, Socket etc. These classes represent a particular group e.g. Reader and Writer classes are for Input/Output operation, Socket and ServerSocket classes are for networking etc and so on. So, Sun Microsoft has subcategorized the java package into subpackages such as lang, net, io etc. The Input/Output related classes in io package. Server and ServerSocket classes in net packages and so on.
  • 13. Example of Subpackage: package com.javatpoint.core; class Simple { public static void main(String args[]) { System.out.println("Hello subpackage"); } } To Compile: javac -d . Simple.java To Run: java com.javatpoint.core.Simple Output:Hello subpackage
  • 14. How to send the class file to another directory or drive? The class file of A.java source file in classes folder of c: drive. For example:
  • 15. //save as Simple.java package mypack; public class Simple { public static void main(String args[]) { System.out.println("Welcome to package"); } }
  • 16. To run this program from e:source directory, you need to set classpath of the directory where the class file resides. e:sources> set classpath=c:classes;.; e:sources> java mypack.Simple To Compile: e:sources> javac -d c:classes Simple.java To Run:
  • 17. • Another way to run this program by -classpath switch of java: • The -classpath switch can be used with javac and java tool. • To run this program from e:source directory, you can use - classpath switch of java that tells where to look for class file. For example: • e:sources> java -classpath c:classes mypack.Simple Output:Welcome to package
  • 18. There are two ways to load the class files temporary and permanent. Ways to load the class files or jar files •Temporary •By setting the classpath in the command prompt •By -classpath switch •Permanent •By setting the classpath in the environment variables •By creating the jar file, that contains all the class files, and copying the jar file in the jre/lib/ext folder.