Chapter9 PartI

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

description

chapter

Transcript of Chapter9 PartI

UNIT- ICHAPTER IN BOOK- 9

PART-I

-K. Indhu

K. INDHU

SYLLABUS COVERED HERE• Packages

K. INDHU

GOALS1. Components of Java Source Code2. Package Introduction3. Package Syntax4. Package Compile & Run5. How Packages are stored ?6. Package Example7. Access Protection8. An Access Example

K. INDHU

COMPONENTS OF JAVASOURCE 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 sub-packages.

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]];

• The package keyword is 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 example-• package 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• Java addresses 4 categories of visibility for class members:-1. Subclasses in the same package,2. Non-subclasses in the same package,3. Subclasses in different packages,4. 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|*);

– > pkg1 = name of a top-level package,– > pkg2 = name of a subordinate package inside pkg1(outer

package) separated by dot (.).

• Examples->1. import java.util.Scanner;2. import java.io.*;3. import java.util.Date;4. import java.lang.*;

K. INDHU

SO FAR WE STUDIED…• Packages

HAPPY LEARNING!!!