Package in Java
Package in Java
Que:
1. Explain packages.
2. Define package and interface.
3. List out the steps to create a package.
4. Write the syntax to create and import a package.
5. Write the steps to create user defined package.
6. Define Package in Java.
7. State the name of any four inbuilt Java package.
8. Explain concept of user defined package in brief.
Ans:
Definition: A java package is a group of similar types of classes, interfaces and sub-
packages.
Package in java can be categorized in two form, built-in package and user-defined
package.
There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql
etc.
User-defined packages:These are the packages that are defined by the user.
Creating a Package
To create package in java is quite easy; simply include a package command as the first
statement in a java source file.
There can be only one package statement in each source file.
Any classes declared within that file will belong to the specified package.
Package statement defines a name space in which classes are stored.
If a package statement is not used in the class,theclass names are put into the default
package.
Syntax of Defining a Package :
package pkg; //here,pkg is package name
package java.awt.image;
Compile and Run Package Program
First we create a directory myPackage. Name should be same as the name of the
package.
MyClass.java must be saved inside the myPackage directory since it is a part of the
package.
MyClass.java must include “package myPackage” as first statement.
package myPack;
public class MyClass
{
public void getNames(String s)
{
System.out.println(s);
}
}
To compile java package Program:
For example
>javac -d . MyClass.java
The -d switch specifies the destination where to put the generated class file.
You can use any directory name like, d:/abc etc. If you want to keep the package
within the same directory, you can use . (dot).
We need to use fully qualified name e.g. mypack.Simple etc to run the class.
>java mypack.Simple
Importing Packages
The import keyword is used to make the classes and interface of another package
accessible to the current package.
Import statement must be after package statement (if it exists) and before class
deifnition.
Syntax:
Import pkg1[.pkg2].(classname|*);
Here,pkg1 is the name of top-level package and pkg2 is the name of a subordinate
package inside the outer package separated by a dot (.).
Import MyPack.*;
Example:
import myPack.MyClass;
obj.getNames(name);
}
}
Built-in Packages
These packages consist of a large number of classes which are a part of Java API.
Some of the commonly used built-in packages are:
1. java.lang:
o Contains language support classes(e.g classed which defines primitive data
types, math operations). This package is automatically imported.
2. java.io:
o Contains classed for supporting input / output operations.
3. java.util:
o Contains utility classes which implement data structures like Linked List,
Dictionary and support ; for Date / Time operations.
4. java.applet:
o Contains classes for creating Applets
5. java.awt:
o Contain classes for implementing the components for graphical user interfaces
(like button , ;menus etc).
6. java.net:
o Contain classes for supporting networking operations.
Access Modifiers
Que:
1. Explain different visibility controls used in Java.
2. Write short note: Access Protection .
3. Explain different Access Controls in Java.
4. Discuss the various levels of protection available for packages & their implications.
Ans:
As the name suggests access modifiers in Java helps to restrict the scope of a class,
constructor , variable , method or data member.
There are four types of access modifiers available in java:
1. Default – No keyword required
2. Private
3. Protected
4. Public
1. Default:
When no access modifier is specified for a class, method or data member. It is said
to be the default access modifier by default.
package p1;
class Geek
{
void display()
{
System.out.println("Hello World!");
}
}
package p2;
import p1.*;
//This class is having default access modifier
class GeekNew
{
public static void main(String args[])
{
obj.display();
}
}
2. Private:
The private access modifier is specified using the keyword private.
The methods or data members declared as private are accessible only within the
class in which they are declared.
Any other class of same package will not be able to access these members.
Classes or interface can not be declared as private.
In this example, we will create two classes A and B within same package p1. We will
declare a method in class A as private and try to access this method from class B and
see the result.
package p1;
class A
{
private void display()
{
System.out.println("GeeksforGeeks");
}
}
class B
{
public static void main(String args[])
{
A obj = new A();
3. protected:
The protected access modifier is specified using the keyword protected.
The methods or data members declared as protected are accessible within same
package or sub classes in different package.
In this example, we will create two packages p1 and p2. Class A in p1 is made public,
to access it in p2. The method display in class A is protected and class B is inherited
from class A and this protected method is then accessed by creating an object of class
B.
package p1;
//Class A
public class A
{
protected void display()
{
System.out.println("GeeksforGeeks");
}
}
Run on IDE
package p2;
import p1.*; //importing all classes in package p1
//Class B is subclass of A
class B extends A
{
public static void main(String args[])
{
B obj = new B();
obj.display();
}
}
4. public:
The public access modifier is specified using the keyword public.
Classes, methods or data members which are declared as public are accessible from
every where in the program.
There is no restriction on the scope of a public data members.
package p1;
public class A
{
public void display()
{
System.out.println("GeeksforGeeks");
}
}
package p2;
import p1.*;
class B
{
public static void main(String args[])
{
A obj = new A;
obj.display();
}
}