0% found this document useful (0 votes)
3 views12 pages

Java Package

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views12 pages

Java Package

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Java Package

 A java package is a group of similar types of classes, interfaces and sub-packages.


Type of Package:
Advantage of Java Package
1. Java package is used to categorize the classes and interfaces so that they
can be easily maintained.
2. Java package provides access protection.
3. Java package removes naming collision.
Built-in Packages
These packages consist of a large number of classes which are a part of
Java API.Some of the commonly used built-in packages are:
1. java.lang: Contains language support classes(e.g classed which defines
primitive data types, math operations). This package is automatically
imported.
2. java.io: Contains classed for supporting input / output operations.
3. java.util: Contains utility classes which implement data structures like
Linked List, Dictionary and support ; for Date / Time operations.
4. java.applet: Contains classes for creating Applets.
5. java.awt: Contain classes for implementing the components for
graphical user interfaces (like button , ;menus etc).
6. java.net: Contain classes for supporting networking operations.
• Adding a class to a Package : We can add more classes to a created
package by using package name at the top of the program and saving it in
the package directory. We need a new java file to define a public class,
otherwise we can add the new class to an existing .java file and recompile it.
• Subpackages: Packages that are inside another package are the
subpackages. These are not imported by default, they have to imported
explicitly. Also, members of a subpackage have no access privileges, i.e.,
they are considered as different package for protected and default access
specifiers.
Steps for creating Package:
Step1:First we create a directory myPackage (name should be same as the
name of the package).
Step2: Create the MyClass inside the directory with the first statement
being the package names.
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.
Using packagename.*
Example of package that import the packagename.*

•//save by A.java • //save by B.java


•package pack; • package mypack;
•public class A{ • import pack.*;
• public void msg() • class B{
{System.out.println("Hello"); • public static void main(String
args[]){
•}
• A obj = new A();
•}
• obj.msg();
• }
• }
Using packagename.classname
Example of package by import package.classname

• //save by A.java • //save by B.java


• package pack; • package mypack;
• public class A{ • import pack.A;
• public void msg() • class B{
•{ • public static void main(String args[])
• System.out.println("Hello"); •{
•} • A obj = new A();
•} • obj.msg();
• }
•}
Using fully qualified name
Example of package by import fully qualified name

• //save by A.java • //save by B.java


• package pack; • package mypack;
• public class A{ • class B{
• public void msg() • public static void main(String args[])
•{ •{
• System.out.println("Hello"); • pack.A obj = new pack.A();//using
fully qualified name
•}
• obj.msg();
•}
• }
•}

You might also like