Upload
aparnajanwalkar
View
217
Download
0
Embed Size (px)
Citation preview
8/12/2019 5.Packagespptx
1/11
5. Packages
8/12/2019 5.Packagespptx
2/11
Topics:
Need for packages
What are packages; package declaration in
Java
Import statement in Java
How do packages resolve name clashes?
8/12/2019 5.Packagespptx
3/11
Package Organize class name space
Hierarchical division must be reflected in directory structure,i.e.,
packagecyc.sys;
import java.io.*;
class Login { /* in fact, this is cyc.sys.Login*/ }
Default packages (library) -- JDK java.lang - Object, Thread, Exception, String,...
java.io - InputStream, OutputStream, ...
java.net - Networking
support for Socket & URL-based objects
java.awt - Abstract window toolkit defines a simple, restricted windowing API
java.util - Hashtable, Vector, BitSet, Regexp, ..
java.tools - Compilation, Debugging, Documentation
8/12/2019 5.Packagespptx
4/11
Naming a Package
Package name should start with small alpha bateonly.
It can be any user defined name.
If super package is available then it must precedesub package name.
Package statement must be the first statement in
.java file
There can be only one package statement in one
.java file.
8/12/2019 5.Packagespptx
5/11
Accessing package
If a package needs to be access outside currentpackage it must be imported by using import
statement.
The syntax is import
root_pkg.sub_pkg.Class/Interface name.
There can be multiple import statements within
same class.
You need not import java.lang (imported bydefault).
8/12/2019 5.Packagespptx
6/11
User defined package
Create A package:->packagemypack;
publicclassSimple{
publicstaticvoidmain(String args[]){
System.out.println("Welcome to package");}
}
Compiling the package:-> javac -d . Simple.java
Run the package as:-> java mypack.Simple
8/12/2019 5.Packagespptx
7/11
How to access package from another package?
There are three ways to access the package from outside the package:
1. import package.*;2. import package.classname;
3. fully qualified name.
(If you use package.* then all the classes and interfaces of this
package will be accessible but not subpackages.)
EX:
//save by A.java
packagepack;
publicclassA{
publicvoidmsg(){
System.out.println("Hello");
}
}
8/12/2019 5.Packagespptx
8/11
//save by B.java
packagemypack;
importpack.*;
classB{
publicstaticvoidmain(String args[]){
A obj = newA();
obj.msg();
}
}
----OR----
pack.A obj = newpack.A();//using fully qualified name
8/12/2019 5.Packagespptx
9/11
Access specifierspublic:Any Class or any package.
private:class access (same as C++)
protected:sub class access + any class in same
package
Default :package access. static: only one copy for all instances
abstract: left unimplemented
final: not overridden
native: methods implemented in native code.
synchronized: described in Thread
transient: data that is not persistent
8/12/2019 5.Packagespptx
10/11
Comparison
Specifier Class Subclass Package World
private X
protected X X X
public X X X Xpackage X X
8/12/2019 5.Packagespptx
11/11 1
Thank You