Package in Java
A package is a group of similar types of classes, interfaces and sub-packages.
A sub-package in turns divides into classes, interfaces, sub-sub-packages, etc.
Default package is java.lang.*
If you import a package, subpackages will not be imported.
Classes in sub-packages cannot access classes in enclosing package with default access
Advantage of Package
- Package is used to categorize the classes and interfaces so that they can be easily maintained.
- Package provids access protection.
- Package removes naming collision
- //save as Simple.java
- package mypack;
- public class Simple{
- public static void main(String args[]){
- System.out.println("Welcome to package");
- }
- }
compile the Package
javac -d directory javafilename
javac -d . Simple.java
The -d switch specifies the destination where to put the generated class file
d:/abc (in case of windows) etc. If you want to keep the package within the same directory, you can use . (dot).
How to run the Package
packagename.javafilename
java mypack.Simple
In java we have two types of packages. They are
1. predefined or built-in or core packages and
2. user or secondary or custom defined packages
PREDEFINED PACKAGES
Predefined packages are those which are developed by SUN microsystems and are part of jdk
Package name
|
Package description
|
java.lang.*
|
This package is used for achieving the language functionalities such as
conversion of data from string to fundamental data, displaying the result on to
the console, obtaining the garbage collector. This is the package which is by
default imported for each and every java program
|
java.io.*
|
This package is used for developing file handling applications, such as, opening
the file in read or write mode, reading or writing the data, etc
|
java.awt.*
|
This package is used for developing GUI (Graphic Unit Interface) components
such as buttons, check boxes, scroll boxes, etc
|
java.awt.event.*
|
Event is the sub package of awt package. This package is used for providing
the functionality to GUI components, such as, when button is clicked or when
check box is checked, when scroll box is adjusted either vertically or
horizontally.
|
java.applet.*
|
This package is used for developing browser oriented applications. In other
words this package is used for developing distributed programs.
An applet is a java program which runs in the context of www or browser
|
java.net.*
|
This package is used for developing client server applications.
|
java.util.*
|
This package is used for developing quality or reliable applications in java or
J2EE. This package contains various classes and interfaces which improves the
performance of J2ME applications. This package is also known as collection
framework
|
java.text.*
|
This package is used for formatting date and time on day to day business
operations.
|
java.lang.reflect.*
|
Reflect is the sub package of lang package. This package is basically used to
study runtime information about the class or interface. Runtime information
represents data members of the class or interface, Constructors of the class,
types of methods of the class or interface
|
java.sql.*
|
This package is used for retrieving the data from data base and performing
various operations on data base.
|
USER DEFINED PACKAGES:
A user defined package is one which is developed by java programmers to simplify the task
of the java programmers to keep set of classes, interfaces and sub packages which are commonly used.
RULE:
Whenever we create user defined package statement as a part of java program, we must use
package statement as a first executable statement
STEPS for developing a PACKAGE:
i. Choose the appropriate package name, the package name must be a JAVA valid variable name
and we showed ensure the package statement must be first executable statement.
ii. Choose the appropriate class name or interface name and whose modifier must be public.
iii. The modifier of Constructors of a class must be public.
iv. The modifier of the methods of class name or interface name must be public.
v. At any point of time we should place either a class or an interface in a package and give the file
name as class name or interface name with extension .java
How to access package from another package?
There are three ways to access the package from outside the package.
- import package.*;
- import package.classname;
- fully qualified name.
import package.*
- 1.//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();
- }
- }
Output:Hello
2. import package.classname
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();
- }
- }
Output:Hello
3. fully qualified name.
Example of package by import fully qualified name
- //save by A.java
- package pack;
- public class A{
- public void msg(){System.out.println("Hello");}
- }
- //save by B.java
- package mypack;
- class B{
- public static void main(String args[]){
- pack.A obj = new pack.A();//using fully qualified name
- obj.msg();
- }
- }
Output:Hello
Subpackage
Package inside the package is called the subpackage. It should be created to categorize the package further.
We can access data in the package using access specifiers
Different types of accessing
|
Public
|
Protected
|
Default
|
Private
|
From Same class
|
Y
|
Y
|
Y
|
Y
|
From any class in same package
|
Y
|
Y
|
Y
|
N
|
From a subclass in the same package
|
Y
|
Y
|
Y
|
N
|
from a subclass outside the same package
|
Y
|
Y
(inheritance)
|
N
|
N
|
from any non-subclass outside the package
|
Y
|
N
|
N
|
N
|
yes-Y
No-N-
No comments:
Post a Comment