Friday, 16 January 2015

Packages in java


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

Package in Java What Why and Best Practices Example Tutorial
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


  1. //save as Simple.java    
  2. package mypack;  
  3. public class Simple{  
  4. public static void main(String args[]){  
  5.    System.out.println("Welcome to package");  
  6.   }  
  7. }
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.
  1. import package.*;
  2. import package.classname;
  3. fully qualified name.


import package.*
  1. 1.//save by A.java  
  2.  
  3. package pack;  
  4. public class A{  
  5.  public void msg(){System.out.println("Hello");}  
  6. }  
  1. //save by B.java  
  2.  
  3. package mypack;  
  4. import pack.*;  
  5.  
  6. class B{  
  7.  public static void main(String args[]){  
  8.   A obj = new A();  
  9.   obj.msg();  
  10.  }  
  11. }  
Output:Hello
2. import package.classname
Example of package by import package.classname
  1. //save by A.java  
  2.  
  3. package pack;  
  4. public class A{  
  5.  public void msg(){System.out.println("Hello");}  
  6. }  
  1. //save by B.java  
  2.  
  3. package mypack;  
  4. import pack.A;  
  5.  
  6. class B{  
  7.  public static void main(String args[]){  
  8.   A obj = new A();  
  9.   obj.msg();  
  10.  }  
  11. }  
Output:Hello

3. fully qualified name.
Example of package by import fully qualified name
  1. //save by A.java  
  2.  
  3. package pack;  
  4. public class A{  
  5.  public void msg(){System.out.println("Hello");}  
  6. }  
  1. //save by B.java  
  2.  
  3. package mypack;  
  4. class B{  
  5.  public static void main(String args[]){  
  6.   pack.A obj = new pack.A();//using fully qualified name  
  7.   obj.msg();  
  8.  }  
  9. }  
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