0% found this document useful (0 votes)
19 views

9 Packages

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)
19 views

9 Packages

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/ 17

Package

s
Introduction

• Packages act as “containers” for classes.

• A package represents a directory that contain


related group of classes & interface.
Types of Packages

• There are basically only 2 types of


java packages.
• They are as follow :
– Built –in Packages or System Packages
(from Java API)
– User Defined Packages
Java Foundation Packages
• Java provides a large number of classes grouped into different
packages based on their functionality.
• The six foundation Java packages are:
– java.lang
• Contains classes for primitive types, strings, math functions, threads, and
exception
– java.util
• Contains classes such as vectors, hash tables, date etc.
– java.io
• Stream classes for I/O
– java.awt
• Classes for implementing GUI – windows, buttons, menus etc.
– java.net
• Classes for networking
– java.applet
4 • Classes for creating and implementing applets
Creating Packages
• Java supports a keyword called “package” for
creating user-defined packages.

//save as Simple.java
package mypack;
public class Simple{
public static void main(String args[])
{ System.out.println("Welcome to
package");
}
}
Accessing a Package
• There are three ways to access the
package from outside the package.

– import package.*;

– import package.classname;

– fully qualified name.


Using packagename.*

• If you use package.* then all the classes and


interfaces of this package will be accessible
but not subpackages.

• The keyword is used to


import and makethe interface
classes of
package
another
accessible to the current package.
//save by A.java
package pack;
public class A{
public void
msg()
{System.out.pri
ntln("Hello");}
//save by B.java
}
package mypack;
import pack.*;

class B{
public static void main(String args[]){
A obj = new A();
obj.msg(); Output: Hello
}
}
Using packagename.classname
//save by A.java

package pack;
public class
A{
public void
msg()
{System.out.
//save by B.java
println("Hell
package mypack;
o");}
import pack.A;
}
class B{
public static void main(String args[]){
A obj = new A(); Output: Hello
obj.msg();
}
}
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
//save by A.java
package pack;
public class A{
public void
msg()
{System.out.pri
ntln("Hello"); Output: Hello
}
}
//save by B.java
package mypack;
class B{
public static
void
main(String
args[]){
pack.A obj =
new
pack.A();
Compiling and Running
• If you are not using any IDE, you need to follow the
syntax given below:
javac -d directory javafilename

Ex: javac -d . Simple.java

Note:
 -d switch specifies the destination where to put
the generated class file;
 d:/abc;
 want to keep the package within the same directory, you
can use . (dot)]
• For java package program:
– You need to use fully name e.g.
qualified mypack.Simple etc to run
the class.
• To Compile: javac -d . Simple.java

• To Run: java mypack.Simple


Subpackage in java
package com.javacourse.core;
class Simple{
public static void main(String
args[]){
System.out.println("Hello
subpackage");
}
}
To Compile: javac -d . Simple.java
To Run: java com.javacourse.core.Simple

Output: Hello
subpackage
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.


Exercise

You might also like