0% found this document useful (0 votes)
8 views11 pages

Package

Uploaded by

Aswin Gouda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views11 pages

Package

Uploaded by

Aswin Gouda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

A java package is a group of similar types of classes, interfaces and sub-packages.

Think of
it as a folder in a file directory

Package in java can be categorized in two form,


1. built-in package and
2. user-defined package.

Advantages of package:-
a. These are useful to arrange related classes and interfaces into a group. Ex:- all the
classes and interfaces performing input and output operations are kept inside java.io
package.
b. Packages hide the classes and interfaces in a separate subdirectory so that accidental
deletion will not take place.
c. We can use same class names in different packages. Ex- there is a Date class in java.util
as well as java.sql package.
d. A group of package is called a library.
Built in package:-
These are the packages which are already available in java.
There are 14+ built in packages in java.
Ex:-
Java.util, java.io, java.awt, java.sql, java.applet, java.swing , java.net etc.
User defined packages:-
▪ Just like the built in packages , users can also create their own packages.
▪ User defined packages can also be imported into other classes and used exactly same as
pre defined.

For creating user defined package keyword is used.


Syntax:-
package packagename;
package packagename.subpackagename;
Ex:-
//save as Simple.java
package mypack;
public class Simple{
public static void main(String args[]){
System.out.println("Welcome to package");
}
}
Compilation of java package:-
javac -d directory . javafilename
Ex:-
javac -d . Simple.java

The –d tells the java compiler to create a separate sub directory and place the .class file there.
Running a java package program:-
To Compile: javac -d . Simple.java
To Run: java mypack.Simple

o/p:-welcome to package.
Access package from another package:-
There are three ways to access the package from outside the package.

❑ import package.*;
❑ import package.classname;
1. Using packagename.* :-
If you use package.* then all the classes and interfaces of this package will be accessible but
not subpackages.

The import keyword is used to make the classes and interface of another package accessible
to the current package.
Example:-
//save by A.java
package pack;
public class A{
public void msg(){System.out.println("Hello");}
}
//save by B.java
package mypack;
import pack.*;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}

o/p:-hello
2. Using packagename.classname :
Example of package by import package.classname
//save by A.java
package pack;
public class A{
public void msg(){System.out.println("Hello");
}
Public class C{
Public void display()
{
System.out.println(“world”);
}
}
//save by B.java
package mypack;
import pack.A;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
Output:Hello
Interface in a package:-
It is also possible to write interface in a package.
But whenever we create an interface the implementation classes should also be created.

Ex:-
package mypack;
Public interface MyDate{
Void show();
}
Subpackage in java:-
Package inside the package is called the subpackage. It should be created to categorize the
package further.
Example of Subpackage
package com.javatpoint.core;
class Simple{
public static void main(String args[]){
System.out.println("Hello subpackage");
}}

You might also like