0% found this document useful (0 votes)
43 views

Java - Packages: by Madhumeeta Bhatia

This document discusses Java packages. Packages allow grouping of related classes and interfaces to organize code and prevent naming collisions. There are built-in Java packages and user-defined packages that can be created using the "package" keyword. Packages are organized hierarchically based on their names and directory structure. The classpath is used to locate packages and classes during compilation and execution. Packages allow code reuse and access protection between classes.

Uploaded by

Diva Kar
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Java - Packages: by Madhumeeta Bhatia

This document discusses Java packages. Packages allow grouping of related classes and interfaces to organize code and prevent naming collisions. There are built-in Java packages and user-defined packages that can be created using the "package" keyword. Packages are organized hierarchically based on their names and directory structure. The classpath is used to locate packages and classes during compilation and execution. Packages allow code reuse and access protection between classes.

Uploaded by

Diva Kar
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 32

Java Training Program

Java – Packages

By
Madhumeeta Bhatia
Contents

Vijayalakshmi G M Java Packages 2


Contents
1. Introduction
2. Packages
3. Package Categories
4. Java Built-In Package
5. Java UserDefined Package
6. What is Classpath
7. Summary

Vijayalakshmi G M Java Packages 3


Introduction

Vijayalakshmi G M Java Packages 4


Introduction
 The main feature of OOP is its ability to support
the reuse of code:
 Extending the classes (via inheritance)
 Extending interfaces
 The features in basic form limited to reusing the
classes within a program.
 What if we need to use classes from other
programs without physically copying them into the
program under development ?
 In Java, this is achieved by using what is known
as “packages”, a concept similar to “class
libraries” in other languages.

Vijayalakshmi G M Java Packages 5


Java Packages

Vijayalakshmi G M Java Packages 6


Packages
 Packages are Java’s way of grouping a number of
related classes and/or interfaces together into a
single unit. That means, packages act as
“containers” for classes.
 It provides is used to organize classes that belong
to the same files or providing similar functionality.

Vijayalakshmi G M Java Packages 7


Features of a Java package
 The protected members of the classes can be
accessed by each other in the same package.
 It resolves the naming collision for the types it
contains.
 A package may have the following types.
 Interfaces
 Classes
 Enumerated types
 Annotations

Vijayalakshmi G M Java Packages 8


Package - Benefits
 The classes contained in the packages of other
programs/applications can be reused.
 In packages classes can be unique compared
with classes in other packages. That two classes
in two different packages can have the same
name. If there is a naming clash, then classes can
be accessed with their fully qualified name.
 Classes in packages can be hidden if we don’t
want other packages to access them.
 Packages also provide a way for separating
“design” from coding.

Vijayalakshmi G M Java Packages 9


Package – Naming Convention
 Package names are written in all lowercase to
avoid conflict with the names of classes or
interfaces.
 The directory name must be same as the name of
package that is created using "package" keyword
in the source file.
 Before running a program, the class path must be
picked up till the main directory (or package) that
is used in the program.

Vijayalakshmi G M Java Packages 10


Package – Naming Convention
 If we are not including any package in our java
source file then the source file automatically goes
to the default package.
 In general, we start a package name begins with
the order from top to bottom level.
 In case of the internet domain, the name of the
domain is treated in reverse (prefix) order.

Vijayalakshmi G M Java Packages 11


Java Package Names and Directory Structure
 Java Packages are defined by hierarchical
naming pattern.
 Package name consists of words separated by
periods.
 First Part – Main Directory Name
 Remaining Part - Sub contents of the package (Sub
Packages and Classes)

Vijayalakshmi G M Java Packages 12


Package categories in Java

Vijayalakshmi G M Java Packages 13


Build-In Packages in Core Java

Vijayalakshmi G M Java Packages 14


Java Built-in Packages

Vijayalakshmi G M Java Packages 15


Java Built-in Packages

Vijayalakshmi G M Java Packages 16


Java Built-in Packages

Vijayalakshmi G M Java Packages 17


Creating Packages
 Java supports a keyword called “package” for creating
user-defined packages.
 The package statement must be the first statement in a
Java source file (except comments and white spaces)
followed by one or more classes.

 Package name is
“myPackage” and classes are
considered as part of this
package; The code is saved in
a file called “ClassA.java” and
located in a directory called
“myPackage”.

Vijayalakshmi G M Java Packages 18


Package Example

 Create a directory "mypackage" .


 Save the  source file as "HelloWorld.java" in the created directory.
 Set the class path as  set CLASSPATH = .;C:\;        
 Go to the "mypackage" directory and compile the program as
C:\mypackage>javac HelloWorld.java
 Run the program.

Vijayalakshmi G M Java Packages 19


Accessing a Package
 As indicated earlier, classes in packages can be
accessed using a fully qualified name or using a
short-cut as long as we import a corresponding
package.
 The general form of importing package is:
 import package1[.package2][…].classname
 Example:
• import myPackage.ClassA;
• import myPackage.secondPackage
 All classes/packages from higher-level package can be
imported as follows:
• import myPackage.*;

Vijayalakshmi G M Java Packages 20


Accessing a Package

Vijayalakshmi G M Java Packages 21


Steps – compile and Execute the packages
 Compile the Package file
 Javac –d HelloWorld.java
 Set the Classpath (Current working Directory)
 Set CLASSPATH=.;
 Define a example to import the package
 Compile and Execute the program in which the
package is imported.

Vijayalakshmi G M Java Packages 22


What is a Class Path

Vijayalakshmi G M Java Packages 23


What is a class path?
 It is a set of directories where Java class files are
located
 Java runtime searches for class files in the
directories specified in the class path in the order
specified.
 We need to set the classpath to point to that
directory so that when we try to run it, the JVM
will be able to see where our classes are stored.

Vijayalakshmi G M Java Packages 24


Protection and Packages
 All classes (or interfaces) accessible to all others
in the same package.
 Class declared public in one package is
accessible within another. Non-public class is not
 Members of a class are accessible from a
difference class, as long as they are not private
 protected members of a class in a package are
accessible to subclasses in a different class

Vijayalakshmi G M Java Packages 25


Access Protection in Packages

Vijayalakshmi G M Java Packages 26


Access to fields in Java at a Glance

Vijayalakshmi G M Java Packages 27


Create Subpackages (A Package inside another
package)

Vijayalakshmi G M Java Packages 28


Summary

Vijayalakshmi G M Java Packages 29


Summary
 Packages allow grouping of related classes into a
single united.
 Packages are organised in hierarchical structure.
 Packages handle name classing issues.
 Packages can be accessed or inherited without
actual copy of code to each program.

Vijayalakshmi G M Java Packages 30


Resource

Vijayalakshmi G M Java Packages 31


Resource
 http://www.roseindia.net/java/master-java/java-
package.shtml

Vijayalakshmi G M Java Packages 32

You might also like