Practical 09:- What is a Package ?
Write programs to create and use
package from the same package and also from a different package. Also
show the use of access specifiers.
1. Introduction
What is package:-
Packages are used in Java, in-order to avoid name conflicts and to control access of class,
interface and enumeration etc. A package can be defined as a group of similar types of
classes, interface, enumeration or sub-package. Using package it becomes easier to locate
the related classes and it also provides a good structure for projects with hundreds of
classes and other files.
Benefits of using package in java:
1) Java package is used to categorize the classes and interfaces so that they can
be easily maintained.
2) Java package provides access protection.
3) Java package removes naming collision.
4) This packages can be provide reusability of code.
5) We can create our own package or extend already available package.
Types of Java Packages:-
• Built-in Package: Existing Java package for
example java.lang, java.util ,java.io etc.
• User-defined-package: Java package created by user to categorize their
project's classes and interface.
Some of the java built-in functions are:
1) java.lang: Contains language support classes(e.g classed which defines
primitive data types,math operations). This package is automatically imported.
2) java.io: Contains classed for supporting input / output operations.
3) java.util: Contains utility classes which implement data structures like Linked
List, Dictionary and support ; for Date / Time operations.
4) java.applet: Contains classes for creating Applets.
5) java.awt: Contain classes for implementing the components for graphical user
interfaces (like button , ;menus etc).
2. Example of Java Package:-
package pk1;
public class Simple
{
public static void main(String args[])
{
System.out.println("Welcome to package");
}
}
3. How to run java package program in Command line:-
You need to use fully qualified name e.g. mypack.Simple etc to run the class.
To Compile: javac Mycode.Simple.java
To Run: java pk1.Simple
Output: Welcome to package
4. Java File path setup :-
Java Virtual Machine(JVM) uses classpath to locate the class files to execute the code.
If your classpath is not set and the class file is not present in the same directory as your
java file, then JVM will be unable to find the required class file, and it will throw an
error.
Syntax for File path: >java -cp <directory_location> <class name>
5. 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.
//Java Class A with package name PK1
package pk1;
public class A
{
int a;
protected int b;
private int c;
public d;
A()
{
a= -1;
b= -1;
c =-1;
d = -1;
void msg(){
System.out.println(“Hellow this code is inside PK1.“);
}
}
}
//Class B in same Package
package pk1;
public class B extend A
{
int e =0;
int f= 1;
B(){
System.out.println("value of a: " + a);
System.out.println("value of b: " + b);
// System.out.println(“value of c: “ + c); // error bcz c is private variable
System.out.println(“value of d: “ + d);
System.out.println(“value of e: “ + e);
System.out.println(“value of f: “ + f);
}
//Java Class C of same package
package pk1;
class C{
public static void main(String[] args){
A obj_A = new A();
B obj_B = new B();
}
}
/*Output
//Importing class from other package
package mypack; //creating Class B in new package mypack
import pk1.*;
class New_pack
{
public static void main(String args[])
{
A obj = new A();
obj.msg();
}
}
Output
There are four types of Java access modifiers:
• Private: The access level of a private modifier is only within the class. It cannot be
accessed from outside the class.
• Default: The accesslevel of a default modifier is only within the package. It cannot
be accessed from outside the package. If you do not specify any access level, it will
be the default.
• Protected: The access level of a protected modifier is within the package and outside
the package through child class. If you do not make the child class, it cannot be
accessed from outside the package.
• Public: The access level of a public modifier is everywhere. It can be accessed from
within theclass, outside the class, within the package and outside the package.