Packages allow organization of related classes and interfaces into namespaces to avoid naming conflicts. A package is declared with the package keyword and specifies a group of related classes. Packages are arranged in a hierarchical manner with subpackages defined using dot notation. Key benefits of packages include easy identification of related classes, avoidance of naming clashes, and access protection control.
3. Packages
• What is a package?
• We already know how to organize our
files
• We use directories for doing so
• The advantage is
• Easy maintenance of files
• If we want to organize our java class files
• System provides a facility
• Packages
3
4. Packages Contd..
• Package
• Name provided to a group of classes
• A collection of related classes and
interfaces
• A namespace management tool provided
by Java
4
5. Packages Contd..
• Directory structures that organize classes and
interfaces
• Mechanism for software reuse
• Can be compared to directories or folders
• Files : Directories :: Class files : Packages
5
5
6. Naming Convention
• Follows standard Java naming rules
• Case sensitive
• Generally written in small case
• Every name must be unique
• Packages in the Java language itself begin with
java
6
7. Naming Convention Contd..
• Prefix a domain name to the preferred package
name
• Avoids name conflict when used in Internet
• for example, com.company.region.package
• com.ibm.in.banking
7
8. Packages
Java packages are classified in to two types :
• Java API packages ( system defined packages)
• User defined packages
8
9. Packages Contd..
•The packages are organized in a hierarchical structure as
shown in the figure
java. awt
Package containing awt package
COLOUR
GRAPHICS
Package containing AWT classes
FONT
…
IMAGE AWT classes containg methods
Hierarchical representation of java.awt
package. The package named java contains the
package awt , which in turn contains various
Fig. 26.1 classes required for implementing graphical
user interface
9
10. System defined packages
• Some examples
• java.io
• java.util
• java.lang
• java.awt
• java.awt.event
• java.sql
• java.net
• java.math
• java.io
10
11. Package Statement
• Used to create a package
• Should be the first statement in a Java source
file
• Syntax
– package nameOfPackage;
Keyword
User-defined name
• eg. : package cis; // cis – college information system
• nameOfPackage is a sequence of names separated by .
(dot)
11
12. Package Statement Contd..
• Java supports the package hierarchy
• Specified by multiple names in a package
statement, separated by . (dot)
– eg. : package firstPackage.secondPackage;
• Advantage is
– Group related classes into a package and
then group the related packages into a larger
package
12
13. Packages
Example :
package firstPpackage ; // package declanation
public class firstClass // class definition
{
----------- ( body of class )
-----------
}
• Here the package name is ‘firstPackage’
• The class ‘firstClass’ is now considered a part of this
package
• This listing would be saved as a file called firstClass.java
• When the source file is compiled, java compiler will
create a class file and store it in the ‘firstPackage’
directory 13
14. Example Program
// A simple package
package coreBank;
class Balance { package statement
String name;
double bal;
Balance(String n, double b) {
name = n;
bal = b;
}
14
15. Example Program Contd..
void show() {
if(bal<0)
System.out.print("-->> ");
System.out.println(name + ": $" + bal);
}
}
15
16. Example Program Contd..
package coreBank;
public class AccountBalance {
public static void main(String args[]) {
Balance current[] = new Balance[3];
current[0] = new Balance("K. J. Fielding", 123.23);
current[1] = new Balance("Will Tell", 157.02);
current[2] = new Balance("Tom Jackson", -12.33);
for(int i=0; i<3; i++) current[i].show();
}
}
16
17. Compiling and Executing
• On compilation
• The package statement creates
directory(folder) with the name of the package
• That is, folder named ‘coreBank’ is created
• While executing
• Qualify the class name with its package name
• That is, coreBank. AccountBalance
17
18. Compiling and Executing Contd..
• Compiling
• javac AccountBalance.java
• Executing
• java coreBank.AccountBalance
Class name to be qualified with its package
18
19. Default Package
• System provides when you do not create one
• It is the current working directory
19
20. Advantages of Packages
• Easily determine how classes and
interfaces are related
• Avoids namespace conflict
• Implement application-wise access
protection
• Provides a mechanism for class reuse
20
21. Summary
• In this class we have discussed
• Package
• Naming convention
• Creating a package
• Concepts of classpath
21
22. Frequently Asked Questions
1. What is a package ?
2. What are Java’s system defined packages ?
3. What are the advantages of packages ?
4. How to set classpath for your Java
application ?
22
23. Quiz
1.An package is a collection of
1. classes
2. Interfaces
3. editing tools
4. classes and interfaces
23
24. Quiz Contd..
2.Which keyword is used to define package
1. packages
2. package
3. packaging
4. directory
24
25. Quiz Contd..
3.A class path specifies
1. A list of paths for java source files
2. A list of paths for image files
3. A list of paths for the required classes and
zip files
4. None
CM604.26 25
26. Quiz Contd..
4.Which of the following statements is true ?
1. There can be any number of package
statements in a java file
2. Package statement can be any where in the
java file
3. There can be only one package statement in
a java file
4. None
CM604.26 26