Thursday 15 September 2016

Package in Java

Package

package
  • A package represents group of classes, interfaces and sub packages.
  • By using package keyword we can create a package.
Advantage
  • Re-usability, develop the project with less time.
Types
    1. Predefined package
    2. User defined package
1. Predefined package
  • Predefined packages means these packages are already defined with Java team.
Example : java.lang package
               java.util  package
               java.io    package
               java.sql   package

1. User defined package
  • User defined packages means these packages are going to define with Java developers.
Creating User defined package
--------------------------------------------------------------------------------------------
Program         :       Example on package
Program name  :       WebSite.java
Output             :         
      Welcome to package
      This is nireekshan website for tutorial

package com;
public class WebSite
{
public static void main(String args[])
{
System.out.println("Welcome to package");
System.out.println("This is nireekshan website for tutorial");
}
}

Compile       :         javac -d WebSite.java
Run             :         java com.WebSite
Output        :         
     Welcome to package
     This is nireekshan website for tutorial
--------------------------------------------------------------------------------------------
Points to know the compiling syntax,
-d option = Java compiler will create sub directory and keeps the .class file there.
. (dot) = create the package in current directory.
--------------------------------------------------------------------------------------------
Accessing the package : Three ways
  1. import packagename.*;
  2. import packagename.classname;
  3. Fully Qualified name
Note : import is a keyword used to access the package.

1. import packagename.*;
  • If we want to get all the classes and interfaces then we need to use packagename.*;
  • In this way we can not get sub package names.
--------------------------------------------------------------------------------------------
Program         :       Example to access the package with packagename.*;
Output            :         
      Its one package and class First

//save as First.java

package one;
public class First
{
public void method1()
{
System.out.println("Its one package and class First");
}
}
Compile      :          javac -d . First.java
Run            :         Please dont run this class, it has no main method

//save as Second.java

package two;
import one.*;
class Second
{
public static void main(String args[])
{
First first = new First();
first.method1();
}
}

Compile      :          javac -d . Second.java
Run            :          java two.Second
Output       :         
     Its one package and class First
--------------------------------------------------------------------------------------------
2. import packagename.classname;
  • In this way we can access only specific class names.
--------------------------------------------------------------------------------------------
Program         :       To access the package with packagename.classname;
Output            :         
      Its one package and class First

//save as First.java

package one;
public class First
{
public void method1()
{
System.out.println("Its one package and class First");
}
}
Compile      :          javac -d . First.java
Run            :         Please dont run First class, it has no main method

//save as Second.java

package two;
import one.First;
class Second
{
public static void main(String args[])
{
First first = new First();
first.method1();
}
}

Compile      :          javac -d . Second.java
Run            :          java two.Second
Output       :         
     Its one package and class First
--------------------------------------------------------------------------------------------
3. Fully Qualified name
  • Here import keyword is not required.
  • In this way directly we can access the packaged classes.
--------------------------------------------------------------------------------------------
Program         :       To access the package with fully qualified name;
Output            :         
      Its one package and class First

//save as First.java

package one;
public class First
{
public void method1()
{
System.out.println("Its one package and class First");
}
}
Compile      :          javac -d . First.java
Run            :         Please dont run First class, it has no main method

//save as Second.java

package two;
class Second
{
public static void main(String args[])
{
one.First first = new one.First();
first.method1();
}
}

Compile      :          javac -d . Second.java
Run            :          java two.Second
Output       :         
     Its one package and class First
--------------------------------------------------------------------------------------------
sub package
  • Package inside package is called sub package.
--------------------------------------------------------------------------------------------
Program         :       Creating sub packages
Program name  :       Test.java
Output            :         
      This is sub package

package three.four.five;
public class Test
{
     public void method1()
     {
          System.out.println("This is sub package");
     }

public static void main(String args[])
{
Test test= new Test();
test.method1();
}
}

Compile      :          javac -d . Test.java
Run            :          java three.four.five.Test
Output       :         
     This is sub package
--------------------------------------------------------------------------------------------
Note
  • There can be only one public class in a java source file and it must be saved by the public class name.
  • save below program as Three.java otherwise it leads to Compilte Time Error
Example:

      class One
      {
      }
      class Two
      {
      }
      public class Three
      {
      }
Access Modifiers
  • We can use these to access the data members and methods,here few of having restrictions and few of having not.
      • private
      • public
      • protected
      • default
public
  • Its a global scope, Its possible to access the data members and methods of class where ever it is.
private
  • Its not a global scope, Its not possible to access the data members and methods of class where ever it is.
  • so, please forget the concept like to access if it is private data members and methods of a class.
protected
  • Its possible to access the data members and methods of class in the same package but not in another package.
default
  • Its possible to access the data members and methods of class in the same package but not in another package.
  • If we didn't specify the scope then default will come into the picture.
Fun point
Nani : So, what is the package?
Sridhar  : While i was in job trails i asked 5.0 package to every company.
Nani : Hello i am asking about java package.
Sridhar  : Oh sorry that thing we can learn now.

Thanks for your time.
Nireekshan