Top Banner
UNIT- I CHAPTER IN BOOK- 9 PART-I -K. Indhu
16
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter9 PartI

UNIT- ICHAPTER IN BOOK- 9

PART-I

-K. Indhu

Page 2: Chapter9 PartI

K. INDHU

SYLLABUS COVERED HERE• Packages

Page 3: Chapter9 PartI

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

Page 4: Chapter9 PartI

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)

Page 5: Chapter9 PartI

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.

Page 6: Chapter9 PartI

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.

Page 7: Chapter9 PartI

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

Page 8: Chapter9 PartI

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.

Page 9: Chapter9 PartI

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.

Page 10: Chapter9 PartI

K. INDHU

PACKAGE EXAMPLE

Page 11: Chapter9 PartI

K. INDHU

PACKAGE EXAMPLE

Page 12: Chapter9 PartI

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.

Page 13: Chapter9 PartI

K. INDHU

AN ACCESS EXAMPLE

Page 14: Chapter9 PartI

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.*;

Page 15: Chapter9 PartI

K. INDHU

SO FAR WE STUDIED…• Packages

Page 16: Chapter9 PartI

HAPPY LEARNING!!!