packages-in-java
packages-in-java
1
Packages: Putting
Classes Together
• One of the main features of OOP is its ability to reuse
the code already created.
• One way is to inheritance – limited to reusing the
classes within the program.
2
Packages in java
• Packages are java’s way of grouping a variety of classes
and/ or interfaces together.
• Examples of packages:
lang, awt, util, applet, javax, swing, net, io, sql,etc.
3
Advantages of packages
1. The classes contained in the package can be
easily reused.
5
Built-in packages
• They are also called as Java API Packages.
• Java API provides a large number of classes grouped
into different packages according to functionality.
6
Java API Packages
• Java.lang: lang stands for language.
• This package has primary classes and interfaces essential for
developing a basic Java program.
• It consists of wrapper classes , String, SttringBuffer,
StringBuilder classes to handle strings, thread class.
• Java.applet:
• applets are programs which come from a server into a client
and get executed on the client machine on a network. Applet
class of this package is useful to create and use applets.
8
Using System packages
• The package name java contains package awt, which
contains various classes required for implementing
GUI. java
awt
Color
Graphics
javajavajava
FONT
Images
double y=java.lang.Math.sqrt(x);
Class
name
Package Method
Name name
11
User-Defined packages:
• The users of the Java language can also create
their own packages.
12
Creating Packages
• package packagename; //to create a package
• package packagename.subpackagename;
• //to create a sub package within a package.
e.g.: package pack;
• The first statement in the program must be package
statement while creating a package.
• While creating a package except instance variables,
declare all the members and the class itself as
public then only the public members are available
outside the package to other programs.
13
A program to create a package pack
with Addition class.
package pack;
public class Addition
{
private double d1,d2;
public Addition(double a, double b) {
d1 = a;
d2 = b; }
public void sum()
{ System.out.println ("Sum is : " + (d1+d2) ); }
}
• Compile and save .java and .class file in pack
directory (folder).
14
Using Package
• Write a java program to use the already created package.
import pack.Addition;
class Use
{
public static void main(String args[])
{
Addition A=new Addition(10.5,5.2);
A.sum();
}
}
15
Steps to create a
package in java
1. Declare the package at the beginning of a file using
form
package packagename;
2. Define a class that is be put in the package and
declare it public.
3. Create a subdirectory under the directory where main
source files are stored.
4. Store the listing as the classname.java file in the
subdirectory created.
5. Compile the file. This creates .class file in the
subdirectory.
16
• Access Protection
Subclass in
same package Yes Yes Yes Yes No
Other classes in
same package Yes Yes Yes No No
Subclasses in
other package Yes Yes No Yes No
Non subclasses
in other Yes No No No No
package
17
Static Import
• Eliminates the need of qualifying a static member with
the class name.
• The static import declaration is similar to that of import.
• We can use static import statement to import static
members from classes and use them without qualifying
class name.