Upload
myrajendra
View
294
Download
0
Embed Size (px)
DESCRIPTION
Citation preview
1
Packages and Classpath
Objective :
On completion of this period, you would be able to learn• Packages• Concept of classpath
2
Recap
Final class
• Classes that can not be inherited
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
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
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
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
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
8
Packages
Java packages are classified in to two types :
• Java API packages ( system defined packages)
• User defined packages
9
•The packages are organized in a hierarchical structure as shown in the figure
COLOUR
GRAPHICS
FONT
IMAGE
…
java. awt
Package containing awt package
Package containing AWT classes
AWT classes containg methods
Hierarchical representation of java.awt package. The package named java contains the package awt , which in turn contains various classes required for implementing graphical user interface
Fig. 26.1
Packages Contd..
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
11
Package Statement
• Used to create a package• Should be the first statement in a Java source file• Syntax
– package nameOfPackage;
KeywordUser-defined name
• eg. : package cis; // cis – college information system• nameOfPackage is a sequence of names separated by . (dot)
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
13
PackagesExample :
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
14
Example Program
// A simple package
package coreBank;
class Balance {
String name;
double bal;
Balance(String n, double b) {
name = n;
bal = b;
}
package statement
15
Example Program Contd..
void show() {
if(bal<0)
System.out.print("-->> ");
System.out.println(name + ": $" + bal);
}
}
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();
}
}
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
18
Compiling and Executing Contd..
• Compiling • javac AccountBalance.java
• Executing • java coreBank.AccountBalance
Class name to be qualified with its package
19
Default Package
• System provides when you do not create one• It is the current working directory
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
21
Summary
• In this class we have discussed• Package • Naming convention• Creating a package• Concepts of classpath
22
Frequently Asked Questions1. 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 ?
23
1.An package is a collection of
1. classes2. Interfaces3.editing tools4.classes and interfaces
Quiz
24
2.Which keyword is used to define package
1.packages2.package3.packaging4.directory
Quiz Contd..
CM604.26 25
3.A class path specifies
1.A list of paths for java source files2.A list of paths for image files3.A list of paths for the required classes and
zip files4.None
Quiz Contd..
CM604.26 26
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
Quiz Contd..