CORE JAVA: Packages

Packages

  • Containers for classes
  • Provides visibility mechanism
  • Packages are a way of grouping variety of classes and/or interfaces together
  • This grouping is usually done according to functionality
  • Thus packages act as containers for classes

Java packages are classified as
–Java API packages (System Packages)

–User-defined packages


Java API packages (System Packages)

Java API(Application Program Interface) provides large numbers of classes grouped into different packages according to functionality. Most of the time we use the packages available with the Java API. The following figure shows the system packages that are frequently used in the programs.

h

pack.png

java.sql :

Provides the API for accessing and processing data stored in a data source (usually a relational database) using the Java programming language.

java.Swing:

It is a part of Java Foundation Classes (JFC) that is used to create window-based applications. It is built on the top of AWT (Abstract Windowing Toolkit) API and entirely written in java.Unlike AWT, Java Swing provides platform-independent and lightweight components.The JavaFX.swing package provides classes for java swing API such as JButton, JTextField, JTextArea, JRadioButton, JCheckbox, JMenu, JColorChooser etc.


User-defined packages

Steps of creating User Defined Packages Java and using them:

  1. Create a package with a .class file
  2. set the classpath from the directory from which you would like to access. It may be in a different drive and directory. Let us call it as a target directory.
  3. Write a program and use the file from the package.

Defining a Package:

package pkg;
  • Include a package command as the first statement in a Java source file
  • Any classes declared within that file will belong to the specified package
  • The package statement defines a name space in which classes are stored
  • If package statement is omitted, class names are put into the default package, which has no name
  • Java uses file system directories to store packages
  • For example, the .class files for any classes we declare to be part of MyPackage must be stored in a directory called MyPackage

package pkg1;
public class ClassA
{
public void displayA()
{
System.out.println(“Class A”);
}
}

  1. Save this file as ClassA.java and store in the subdirectory pkg1
  2. Compile this file.
  3. The resultant ClassA.class will be stored in the same directory

import pkg1.ClassA;

class PackageTest1

{

public static void main (String[] args)

{

ClassAobjA= new ClassA();

objA.displayA();

}

}

  1. Save this file as PackageTest1.java in the directory of which pkg1 is a subdirectory
  2. Compile and run the file
  • We can create a hierarchy of packages :
    package pkg1[.pkg2[.pkg3]];
  • A package hierarchy must be reflected in the file system. For example, a package declared as package java.awt.image;    needs to be stored in  java\awt\image  in a Windows environment.

Classpath:

How does the Java run-time system know where to look for packages that we create?

1.Java run-time system uses the current working directory as its starting point.

Thus, if the package is in a subdirectory of the current directory, it will be found

2.We can specify a directory path or paths by setting the CLASSPATH environmental variable.

Consider the following package specification:
package MyPack;

In order for a program to find MyPack, one of two things must be true.

1.Either the program can be executed from a directory immediately above MyPack, or

2.the CLASSPATH must be set to include the path to MyPack

set classpath=D:\MyPrograms\Java\MyPack

Access modifiers in java

There are two types of modifiers in Java: access modifiers and non-access modifiers.

The access modifiers in java specify accessibility (scope) of a data member, method, constructor or class.

There are 4 types of java access modifiers:

  1. private
  2. default
  3. protected
  4. public

There are many non-access modifiers such as static, abstract, synchronized, native, volatile, transient etc. Here, we will learn access modifiers.

1) private access modifier

The private access modifier is accessible only within the class.

Simple example of private access modifier

In this example, we have created two classes A and Simple. A class contains private data member and private method. We are accessing these private members from outside the class, so there is compile time error.

pack1.png

Role of Private Constructor

If you make any class constructor private, you cannot create the instance of that class from outside the class. For example:

pack1.png

Note: A class cannot be private or protected except nested class.

2) default access modifier

If you don’t use any modifier, it is treated as default by default. The default modifier is accessible only within the package.

Example of default access modifier

In this example, we have created two packages pack and mypack. We are accessing the A class from outside its package since A class is not public so it cannot be accessed from outside the package.

pack1.png

3) protected access modifier

The protected access modifier is accessible within the package and outside the package but through inheritance only.The protected access modifier can be applied to the data member, method, and constructor. It can’t be applied to the class.

Example of protected access modifier

In this example, we have created the two packages pack and mypack. The A class of pack package is public, so can be accessed from outside the package. But msg() method of this package is declared as protected so it can be accessed from outside the class only through inheritance.

 

pack.png

Output: Hello

4) public access modifier

The public access modifier is accessible everywhere. It has the widest scope among all other modifiers.

Example of public access modifier

pack1.png

Output: Hello

pack1.png

 

Advertisements

One thought on “CORE JAVA: Packages

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s