Package

  • These are the containers for the classes.
  • They are used to keep the class name space compartmentalized means we can use same name of the class which is in one package can also define in another package.
  • It provides the both a naming and a visibility control mechanism means we can define a class inside a package that are not acceptable to the outside of the package also we can define a class members which can be accessed by other members of the same package

Defining a Package

  • By including a package command as the first statement in the java source file.
  • The classes defined in that file are belong to the specific package.
  • If we did not used the package it will store in default package which don't have name.

General form

package package_name;

ex: package MyPackage;

This statement creates the package called Mypackage.

  • Java uses file system to store packages as it stores. class file for any classes in the directory which name is exactly same as package name.

  • We can not rename the packages without renaming the directory in which classes are stored.

  • We can create a hierarchy of packages which can done by . (period) symbol.

General form

package pkg1[.pkg2[.pkg3]];

Ex: package.java.awt.image;

Finding Packages and Class path

  • As java uses directory to store packages then the java run-time environment should know where the packages are created .
  • Three ways It will automatically checks the directory where the source file is.

  • If not it will uses the class path where that package created.

Ex: If the directory path is C:\MyPrograms\Java\MyPack

then class path is C:\MyPrograms\Java

  1. By using- classpath option with java and javac to specify the path to your classes.

Access Protection

Java provides many levels of protection to allow fine-grained control over the visibility of the variables and methods within classes, sub classes, packages. Java addresses four categories of visibility for class members:

  • Sub classes in the same package
  • Non-sub classes in the same package
  • Subclasses in different packages
  • Classes that are neither in the same package nor sub classes

These categories can be done by private, public, protected, default access modifiers.

private- Only to the members of the class.

public- Can be accessed from any where.

default - Accessed by the sub classes as well as other classes in the same package.

protected - If we want to access to the sub classes in outside of the package but sub classes of the class.

Private No modifier Public Protected
Same class Yes Yes Yes Yes
Same package sub classes No Yes Yes Yes
Same package non- sub classes No Yes Yes Yes
Different package sub classes No No Yes Yes
Different package non sub classes No No Yes No
//protected access to the non-sub classes in the same package
class A 
{
protected int x;
  }


class B {
        B() {
              A a=new A();
              a.x=9;//why we can access this field ?
              System.out.println("The value of x is::"+a.x);
        }
  }

public class Demo
{
  public static void main(String args[])
  {
    B b = new B();

  }
}

Output:

The value of x is::9

Importing Packages

  • Java includes the import statement to bring certain classes, or entire packages, into visibility.

  • In a Java source file, import statements occur immediately following the package statement (if it exists) and before any class definitions.

import pkg1 [.pkg2].(classname | *);

package pack_name; (optional)

class ClassName

{

//members of class

}

import java.util.*;   //All classes in the sub package util
import java.util.Scanner; //Only Scanner class in the util package.
class MyClass extends Scanner
{
//members of class
}

//OR                                                                 

class MyClass extends java.util.Scanner
{
//members;
}

As we can access the public members only to the outside package non-sub class members so Scanner should be declared as public.

results matching ""

    No results matching ""