You are on page 1of 16

UNIT- I

CHAPTER IN BOOK- 9
PART-I
-K. Indhu

SYLLABUS COVERED
HERE

Packages

K. INDHU

GOALS
1.
2.
3.
4.
5.
6.
7.
8.

Components of Java Source Code


Package Introduction
Package Syntax
Package Compile & Run
How Packages are stored ?
Package Example
Access Protection
An Access Example

K. INDHU

COMPONENTS OF JAVA
SOURCE CODE

A Java source file can contain any (or all) of


the following four internal parts:(1) A single package statement (optional)

(2) Any number of import statements


(optional)

(3) A single public class declaration (required)

(4) Any number of classes private to the


package (optional)
K. INDHU

PACKAGE INTRO

The package is both a naming and a visibility


control mechanism.

One can define classes inside a package that are


not accessible by code outside that package.

One can also define class members that are only


exposed to other members of the same package.

DEFINITION-> A java package is a group of


similar types of classes, interfaces and subpackages.
K. INDHU

PACKAGE SYNTAX

The general syntax of the package statement is->


package pkg;
Here, pkg is the name of the package.

For example, the following statement creates a package


called MyPackage->
package MyPackage;

The general form of a multileveled package statement


is shown here:package pkg1[.pkg2[.pkg3]];
Thepackage keywordis used to create a package in
java.
K. INDHU

PACKAGE COMPILE &


RUN

FOR COMPILING PACKAGE IN A DIRECTORY:javac -d directory javafilename


EXAMPLE->
javac -d . Simple.java
. Denotes current working directory,
-d option specifies destination to put generated class file.
FOR RUNNING PACKAGE IN A DIRECTORY:java <package_name>.<class_name>
EXAMPLE->
java mypack.Simple
K. INDHU

HOW PACKAGES
STORED ?

Java uses file system directories to store


packages.

For example, the .class files for any java classes


that are declared to be part of MyPackage must
be stored in a directory called MyPackage.

The directory name must match the package


name exactly with case.

More than one file can include the same package


statement.
K. INDHU

HOW PACKAGES
STORED ?

Most real-world packages are spread across many


files.

The package statement simply specifies to which


package the classes defined in a file belong to.

For examplepackage java.awt.image;


needs to be stored in \java\awt\image in windows
directory.

Thus, packages are mirrored by directories.


K. INDHU

PACKAGE EXAMPLE

K. INDHU

PACKAGE EXAMPLE

K. INDHU

ACCESS PROTECTION

1.
2.
3.
4.

Java addresses 4 categories of visibility for class members:Subclasses in the same package,
Non-subclasses in the same package,
Subclasses in different packages,
Classes that are neither in the same package nor subclasses.

K. INDHU

AN ACCESS EXAMPLE

K. INDHU

IMPORTING PACKAGES

The general form of the import statement is:import pkg1[.pkg2].(classname|*);

1.
2.
3.
4.

> pkg1 = name of a top-level package,


> pkg2 = name of a subordinate package inside
pkg1(outer package) separated by dot (.).

Examples->
import java.util.Scanner;
import java.io.*;
import java.util.Date;
import java.lang.*;
K. INDHU

SO FAR WE STUDIED
Packages

K. INDHU

HAPPY
LEARNING!!!

You might also like