Java Package
Java Package
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.
1) Java package is used to categorize the classes and interfaces so that they
can be easily maintained.
1. //save as Simple.java
2. package mypack;
1
3. public class Simple{
4. public static void main(String args[]){
5. System.out.println("Welcome to package");
6. }
7. }
How to compile java package
If you are not using any IDE, you need to follow the syntax given below:
You need to use fully qualified name e.g. mypack.Simple etc to run the class.
1. import package.*;
2. import package.classname;
3. fully qualified name.
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.
2
Example of package that import the
packagename.*
1. //save by A.java
2. package pack;
3. public class A{
4. public void msg(){System.out.println("Hello");}
5. }
1. //save by B.java
2. package mypack;
3. import pack.*;
4.
5. class B{
6. public static void main(String args[]){
7. A obj = new A();
8. obj.msg();
9. }
10.}
2) Using packagename.classname
If you import package.classname then only declared class of this package
will be accessible.
3
6. public static void main(String args[]){
7. A obj = new A();
8. obj.msg();
9. }
10.}
It is generally used when two packages have same class name e.g.
java.util and java.sql packages contain Date class.
If you import a package, all the classes and interface of that package will be
imported excluding the classes and interfaces of the subpackages. Hence,
you need to import the subpackage as well.
4
Subpackage in java
Package inside the package is called the subpackage. It should be
created to categorize the package further.
Let's take an example, Sun Microsystem has definded a package named java
that contains many classes like System, String, Reader, Writer, Socket etc.
These classes represent a particular group e.g. Reader and Writer classes
are for Input/Output operation, Socket and ServerSocket classes are for
networking etc and so on. So, Sun has subcategorized the java package into
subpackages such as lang, net, io etc. and put the Input/Output related
classes in io package, Server and ServerSocket classes in net packages and
so on.
5
1. //save as Simple.java
2. package mypack;
3. public class Simple{
4. public static void main(String args[]){
5. System.out.println("Welcome to package");
6. }
7. }
To Compile:
To Run:
To run this program from e:\source directory, you need to set classpath of the directory wh
class file resides.
6
To run this program from e:\source directory, you can use -classpath switch
of java that tells where to look for class file. For example:
Output:Welcome to package
Rule: There can be only one public class in a java source file and it must be saved by
the public class name.
7
Java Static Import
The static import feature of Java 5 facilitate the java programmer to access
any static member of a class directly. There is no need to qualify it by the
class name.
8
Access Modifiers in Java
1. Private: The access level of a private modifier is only within the class.
It cannot be accessed from outside the class.
2. Default: The access level 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.
3. 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.
4. Public: The access level of a public modifier is everywhere. It can be
accessed from within the class, outside the class, within the package
and outside the package.
Private Y N N N
Default Y Y N N
Protected Y Y Y N
Public Y Y Y Y
1) Private
The private access modifier is accessible only within the class.
9
Simple example of private access modifier
1. class A{
2. private int data=40;
3. private void msg(){System.out.println("Hello java");}
4. }
5.
6. public class Simple{
7. public static void main(String args[]){
8. A obj=new A();
9. System.out.println(obj.data);//Compile Time Error
10. obj.msg();//Compile Time Error
11. }
12. }
If you make any class constructor private, you cannot create the instance of
that class from outside the class. For example:
1. class A{
2. private A(){}//private constructor
3. void msg(){System.out.println("Hello java");}
4. }
5. public class Simple{
6. public static void main(String args[]){
7. A obj=new A();//Compile Time Error
8. }
9. }
Note: A class cannot be private or protected except nested class.
2) Default
If you don't use any modifier, it is treated as default by default. The default
modifier is accessible only within package. It cannot be accessed from
outside the package. It provides more accessibility than private. But, it is
more restrictive than protected, and public.
10
In this example, we have created two packages pack and mypack. We are
accessing the A class from outside its package, since A class is not public, so
it cannot be accessed from outside the package.
1. //save by A.java
2. package pack;
3. class A{
4. void msg(){System.out.println("Hello");}
5. }
1. //save by B.java
2. package mypack;
3. import pack.*;
4. class B{
5. public static void main(String args[]){
6. A obj = new A();//Compile Time Error
7. obj.msg();//Compile Time Error
8. }
9. }
In the above example, the scope of class A and its method msg() is default so it
cannot be accessed from outside the package.
3) Protected
The protected access modifier is accessible within package and outside
the package but through inheritance only.
The protected access modifier can be applied on the data member, method
and constructor. It can't be applied on the class.
In this example, we have created the two packages pack and mypack. The A class
of pack package is public, so can be accessed from outside the package. But msg
method of this package is declared as protected, so it can be accessed from outside
the class only through inheritance.
1. //save by A.java
11
2. package pack;
3. public class A{
4. protected void msg(){System.out.println("Hello");}
5. }
1. //save by B.java
2. package mypack;
3. import pack.*;
4.
5. class B extends A{
6. public static void main(String args[]){
7. B obj = new B();
8. obj.msg();
9. }
10. }
4) Public
The public access modifier is accessible everywhere. It has the widest
scope among all other modifiers.
1. class A{
2. protected void msg(){System.out.println("Hello java");}
3. }
4.
5. public class Simple extends A{
6. void msg(){System.out.println("Hello java");}//C.T.Error
7. public static void main(String args[]){
8. Simple obj=new Simple();
9. obj.msg();
10. }
11. }
The default modifier is more restrictive than protected. That is why, there is a
compile-time error.
12
Encapsulation in Java
Encapsulation in Java is a process of wrapping code and data together
into a single unit, for example, a capsule which is mixed of several
medicines.
We can create a fully encapsulated class in Java by making all the data
members of the class private. Now we can use setter and getter methods to
set and get the data in it.
13
22. //setting value in the name member
23. s.setName("vijay");
24. //getting value of the name member
25. System.out.println(s.getName());
26. }
27. }
Read-Only class
Write-Only class
14