Reading:  

Quick introduction to Object oriented concepts in Java - Part 2 of series


Packages

A package is a group of same types of interfaces, classes, and sub-packages. Packages are used to control access, to prevent naming conflicts, to make locating and usage of interfaces, classes, annotation and enumerations easily. 

Package is categorized into two forms:

  • Built-in packages
  • User-defined packages

Built-in packages: Standard packages which come as a part of JRE (Java Runtime Environment) Built-in packages such as net, lang, java, javax, io, awt, util, swing, sql etc.

User-defined packages: Packages defined by programmers to bundle group of related classes 

Advantages of Packages:

  • Packages removes naming collision
  • Packages are used to group the interfaces and classes so that they can easily maintain
  • Packages provide access protection

 Sub-Package:

 A Package inside the package is known as sub-package. 

Package Hierarchy:

Common used built-in packages are:

Package name

Description

Java.applet

Contains classes for Applets creation

Java.net

Contains classes for supporting networking operations

Java.util

Contains utility classes which implements data-structure like Linked-List, HashTable, Dictionary etc

Java.sql

Contains classes for supporting SQL operations

Java.awt

Contains classes for implementing the components of graphical user interface like button, menu etc

Java.io

Contains classes for supporting operations like input/output

Java.lang

Contains language support classes which defines primitive data-types, math operations etc

 

Creating a package:

To create a package, we need to choose a package name and keep a package statement as very first statement inside the source file which contains the interfaces, classes, annotation, enumerations etc. The keyword “package” is used for the creation of a package.

Syntax of Package:

package mypack; //package creation
public class A {
	public static void main(string[] args) {
		System.out.println(“Inside main method”);
	}
}

 

If a package is not used then interfaces, class, enumerations will be saved into an unnamed package

  • To compile the package if not using IDE:

       javac –d  javafilename  

        -d: specifies the folder of destination where to keep the generated class

Example:    javac –d Simple.java 

  • To run package if not using IDE:

            java mypack.Simple

How to access package from other package?

There are 3 ways to access package from outside the package:

  • import package.*;
  • import package.classname;
  • Fully qualified name;

 

Using package name:

If we use package.* then all the interfaces and classes of this package will be accessible but not sub-packages.

The keyword “import” which provides the facility to access the interfaces and classes of another package to the current package

Example of import:

package mypack;
public class A {
	public void messageDisplay() {
		System.out.println(“Inside mypack package”);
	}
}

// import mypack

package packexm; import mypack.*; //importing mypack package class B { public static void main() { System.out.println(“Inside main method of package packexm”); A ref = new A(); ref.messageDisplay(); } }

Output

Inside main method of package packexm
Inside mypack package

Using package.classname:

Example:

package mypack;
public class A {
	public void messageDisplay() {
		System.out.println(“Inside mypack package”);
	}
}

package packexm;
import mypack.A; //importing mypack package using class name
class B {
	public static void main() {
		System.out.println(“Inside main method of package packexm”);
		A ref = new A();
		ref.messageDisplay();
	}
}

The Output is:

Inside main method of package packexm
Inside mypack package 

 

Set CLASSPATH System Variable using command line:

The commands for Linux and Windows used to display the current CLASSPATH variable:

Windows:    C:\> set CLASSPATH
UNIX:    % echo $CLASSPATH                    

To set the CLASSPATH:

Windows:  set CLASSPATH=C:\java_examples\classes
UNIX:  % CLASSPATH=/home/java_exampels/classes; export CLASSPATH

To delete the contents of the CLASSPATH variable:

Windows:   C:\> set CLASSPATH=
UNIX:   % unset CLASSPATH; export CLASSPATH

 

Description

This tutorial will introduce you to Object oriented concepts in Java. Basic definitions and learning with code examples.

This tutorial has subdivided into 7 parts as listed below

  1. Understanding Inheritance    
  2. Understanding Overrides
  3. Understanding Polymorphism
  4. Understanding Abstraction
  5. Understanding Encapsulation
  6. Understanding Interfaces
  7. Understanding Packages

Let us know if you found any error in this tutorial. Your feedback is welcome as always.



Audience

Students seeking a quick overview on Java Programming language and its Object oriented features.

Learning Objectives

Learn Object Oriented concepts in Java language.

Author: Subject Coach
Added on: 10th Mar 2015

You must be logged in as Student to ask a Question.

None just yet!