Get the online training

  • Every class in the Java library belong to a package.
  • In the Java API, classes are grouped into packages.
  • A class has a full name, which is a combination of the package name and the class name.
    For example, the class ArrayList is actually java.util.ArrayList.
  • To use a class in a package other than java.lang, you must tell Java the full name of the class.
  • Packages are important for three main reasons:
  • Firstly, they help in the overall organization of a project or library.
  • Secondly, packages give you a name-scoping, to help to prevent collisions
    if many programmers in a company decide to make a class with the same name.
  • Thirdly, packages provide a level of security, because you can restrict the code,
    which you write so that only other classes in the same package can access it.
  • To put a class in a package, put a package statement at the top of the source code file,
    before any import statement like package com.mypack;.
  • To be in a package, a class must be in a directory structure that exactly matches the package structure.
  •  For a class, com.mypack.Book, the Book class must be in a directory named mypack,
    which is in a directory named com.
  • Organizing your classes into packages prevents naming collisions with other classes,
    if you preponed your reverse domain name on to the front of a class name.
Import Statement
  • import java.io.*;
  • import java.util.ArrayList;
  • import java.util.Date;
  • import java.util.Properties;
The import statements must come right after the package statement, before the class statement.

Purpose of import statement:

  • Because of the rigid source code naming convention, the Java compiler can easily find
    the corresponding source or class files just from the fully qualified name
    of a package and class.
  •  This can be illustrated with the following example:
    java.util.ArrayList myList = new java.util.ArrayList(50);
  • The alternative to this long-winded style of coding (as shown earlier), is to use import statements like the following:
    import java.util.ArrayList;

    ArrayList myList = new ArrayList(50);

To derive a class from an external superclass, you must first import the
superclass using the import statement.


package pack2;

public class manager {

  protected  manager(){}
  public static void main(String[] args) {

import pack2.manager;

class client1 extends manager
  public static void main(String[] args) {
    manager m=new manager();