Dr.
SNS Rajalakhsmi College of Arts and Science
(An Autonomous Co-Education Institution)
Coimbatore – 641049
[Link]
PACKAGES IN JAVA
Presentation by
[Link]
Assistant Professor
Department of Information Technology
[Link]@[Link]
Department of
IT
Information
Technology
Drsnsrcas/CPreprocessor/April 27, 2024 Slide No : 1
INTRODUCTION
• 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.
• The benefits of organising classes into packages are:
- 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.
Department of
[Link]
IT Information
Technology
Drsnsrcas/CPreprocessor/April 27, 2024 Slide No : 2
Java provides a large number of classes groped into different packages
based on their functionality
The six foundation Java packages are:
- [Link] :
Contains classes for primitive types, strings, math functions,
threads, and exception.
- [Link] :
Contains classes such as vectors, hash tables, date etc.
- [Link] :
Stream classes for I/O .
- [Link] :
Classes for implementing GUI – windows, buttons, menus etc.
- [Link] :
Classes for networking .
- [Link] :
Classes for creating and implementing app.
Department of
[Link]
IT
Information
Technology
Drsnsrcas/CPreprocessor/April 27, 2024 Slide No : 3
Predefined Macros
ANSI C defines a number of macros. Although each one is available for use in
programming, the predefined macros should not be directly modified.
Macro & Description
DATE__The current date as a character literal in "MMM DD YYYY" format.
TIME__The current time as a character literal in "HH:MM:SS" format.
FILE__This contains the current filename as a string literal.
LINE__This contains the current line number as a decimal constant.
STDC_Defined as 1 when the compiler complies with the ANSI standard.
Department of
[Link]
IT Information
Technology Drsnsrcas/CPreprocessor/April 27, 2024
Slide No : 4
Example
The packages are organised in a hierarchical structure .For
example, a package named “java” contains the package
“awt”, which in turn contains various classes required for
implementing GUI (graphical user interface).
Department of
[Link]
IT Information
Technology
Drsnsrcas/CPreprocessor/April 27, 2024 Slide No : 5
Accessing Classes from Packages
There are two ways of accessing the classes stored in
packages:
Using fully qualified class name
[Link](x);
Import package and use class name directly
import [Link]
[Link](x);
Selected or all classes in packages can be imported:
import [Link];
import package.*;
Implicit in all programs: import [Link].*;
package statement(s) must appear first
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 myPackage;
public class ClassA {
// class body
}
class ClassB {
}
Package name is “myPackage” and classes are
considered as part of this package; The code is saved in a file called
“[Link]” and located in a directory called “myPackage”.
Department of
[Link]
IT Information
Technology Drsnsrcas/CPreprocessor/April 27, 2024
Slide No : 7
USING PACKAGE
Let us store the code listing below in a file named“[Link]” within subdirectory
named “myPackage”within the current directory (say “abc”).
package myPackage;
public class ClassA {
// class body
public void display()
{
[Link]("Hello, I am ClassA");
}
}
class ClassB {
// class body
}
Department of
[Link]
IT Information
Technology Drsnsrcas/CPreprocessor/April 27, 2024
Slide No :8
Compiling and Running
en [Link] is compiled, the compiler compiles it and places .class file in
current directly. If .class of ClassA in subdirectory “myPackage” is not found,
it comples ClassA also.
Note: It does not include code of ClassA into ClassX ✞
When the program ClassX is run, java loader looks for [Link] file in a
package called “myPackage” and loads it.
Department of
[Link]
IT Information
Technology Drsnsrcas/CPreprocessor/April 27, 2024
Slide No :9
USING PACKAGE
Let us store the code listing below in a file named“[Link]” within
subdirectory named“secondPackage” within the current directory (say“abc”).
public class ClassC {
// class body
public void display()
{
[Link]("Hello, I am ClassC");
}
}
Department of
[Link]
IT Information
Technology Drsnsrcas/CPreprocessor/April 27, 2024
Slide No :10
Department of
[Link]
IT
Information
Technology Drsnsrcas/CPreprocessor/April 27, 2024