Packages
• A package is a pack of classes, interfaces and other packages.
• It also represents a directory that contains related group of classes
and interfaces.
• In java we use packages to organize our classes and interfaces.
• We have two types of packages in Java: built-in packages and the
packages we can create also known as user defined package.
Packages
• Packages are useful to arrange related classes and interfaces into a
group. This makes all the classes and interfaces performing the same
task put together in the same package.
• Packages hide the classes and interfaces in a separate subdirectory
so that accidental deletion of classes and interfaces will not take
place.
Advantages of using Package
• Reusability: While developing a project in java, we often feel that there are
few things that we are writing again and again in our code.
• Using packages, you can create such things in form of classes inside a package
and whenever you need to perform that same task, just import that package
and use the class.
• Better Organization: Again, in large java projects where we have several
hundreds of classes, it is always required to group the similar types of classes
in a meaningful package name so that you can organize your project better and
when you need something you can quickly locate it and use it, which improves
the efficiency.
• Name Conflicts: We can define two classes with the same name in different
packages so to avoid name collision, we can use packages.
Different types of packages(Built-in)
• There are packages already available in Java language.
• These packages provide almost all necessary classes, interface and methods
for the programmer to perform any task.
• The programmer need not think of the logic for any task.
• java.lang
• java.util
• java.awt
• java.swing
• java.applet
• java.net
• java.io
User defined Packages
• The users of Java language can also create their own packages.
• They are called as user defined packages.
• User defined packages can also be imported into other classes and used
exactly in the same way as the built in packages.
• To create a package
• package packagename;
• package packagename.subpackagename;
• While creating the class in a package,declare all the members and the class
itself as public except instance variables.
• Only the public members are available outside the package to other programs.
example
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="+(d1+d2));
}
}
• The –d option tells the compiler to create a separate sub directory
and place the .class file there.
• The dot after -d indicates that the package should be created in the
current directory.
import pack.Addition;
class Use1
{
public static void main(String args[])
{
Addition obj=new Addition(10,10.5);
obj.sum();
}
}
Interfaces in a package
• It is also possible to write interfaces in a package.
• Whenever you create an interface the interface classes also should
be declared.
• Create myDate interface in the
package pack1
package pack1;
public interface myDate
{
void showDate();
}
• The Java compiler creates a sub directory with the name pack1 and
stores myDate.class file there.
• This myDate.class file denotes the bytecode of the interface.
• The next step is to create the implementation class for myDate
interface where showDate() method body should be written.
• DateImpl is an implementation class where the body for showDate()
method is written.
• We create an object to Dateclass of java.util package which by default
stores the system date and time.
package pack1;
import pack1.myDate;
import java.util.*;
public class DateImpl
implements myDate
{
public void showDate()
{
Date d=new Date();
System.out.println(d);
}
}
• When the preceding code is import pack1.DateImpl;
compiled DateImpl.class file is class DateDisplay
created in the same package pack1.
{
• DateImpl class contains showDate()
method which can be called and public static void main(String
used in any other program. args[])
• DateDisplay is the class where we {
want to use DateImpl class.
DateImpl obj=new DateImpl();
• So object to DateImpl is created
and the showDate() method is obj.showDate();
called. }
• This displays system date and time. }
Creating subpackage in a package
• We can create a subpackage in a package in the format
• Package pack1.pack2;
• Here we are creating pack2 which is created inside pack1.
• To use the classes and interfaces of pack2 we can write import
statement as
• Import pack1.pack2;
• This concept can be extended to create several sub packages.
package pack3.pack4;
class sample
{
public void show()
{
System.out.println("Welcome");
}
}
import pack3.pack4.sample;
class sample1
{
public static void main(String args[])
{
sample obj=new sample();
obj.show();
}
}
Scope of the members in package
package same;
public class access
{
private int a=1;
public int b=2;
protected int c=3;
int d=4;
}
package same;
import same.access;
public class Baccess
{
public static void main(String args[])
{
access obj=new access();
System.out.println(obj.a);
System.out.println(obj.b);
System.out.println(obj.c);
System.out.println(obj.d);
}
}
package another;
import same.access;
public class Caccess
{
public static void main(String args[])
{
access obj=new access();
System.out.println(obj.a);
System.out.println(obj.b);
System.out.println(obj.c);
System.out.println(obj.d);
}
}
package another;
import same.access;
public class C extends access
{
public static void main(String args[])
{
C obj=new C();
System.out.println(obj.a);
System.out.println(obj.b);
System.out.println(obj.c);
System.out.println(obj.d);
}
}