0% found this document useful (0 votes)
4 views

Java Package

A Java package is a collection of related classes, interfaces, and sub-packages, categorized into built-in and user-defined types. Packages provide advantages such as organization, access protection, and avoidance of naming collisions. The document also covers how to create, compile, and access packages, as well as the use of access modifiers and encapsulation in Java.

Uploaded by

ishikajanit
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Java Package

A Java package is a collection of related classes, interfaces, and sub-packages, categorized into built-in and user-defined types. Packages provide advantages such as organization, access protection, and avoidance of naming collisions. The document also covers how to create, compile, and access packages, as well as the use of access modifiers and encapsulation in Java.

Uploaded by

ishikajanit
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Java Package

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.

Advantage of Java Package

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.

Simple example of java package


The package keyword is used to create a package in java.

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:

1. javac -d directory javafilename

How to run java package program

You need to use fully qualified name e.g. mypack.Simple etc to run the class.

To Compile: javac -d . Simple.java

To Run: java mypack.Simple


Output: Welcome to package
The -d is a switch that tells the compiler where to put the class file i.e. it represents
destination. The . represents the current folder.

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.

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.

Example of package by import


package.classname
1. //save by A.java
2.
3. package pack;
4. public class A{
5. public void msg(){System.out.println("Hello");}
6. }
1. //save by B.java
2. package mypack;
3. import pack.A;
4.
5. class B{

3
6. public static void main(String args[]){
7. A obj = new A();
8. obj.msg();
9. }
10.}

3) Using fully qualified name


If you use fully qualified name then only declared class of this package will
be accessible. Now there is no need to import. But you need to use fully
qualified name every time when you are accessing the class or interface.

It is generally used when two packages have same class name e.g.
java.util and java.sql packages contain Date class.

Example of package by import fully qualified


name
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. class B{
4. public static void main(String args[]){
5. pack.A obj = new pack.A();//using fully qualified name
6. obj.msg();
7. }
8. }

Note: If you import a package, subpackages will not be imported.

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.

The standard of defining package is domain.company.package e.g.


com.javatpoint.bean or org.sssit.dao.

How to send the class file to another directory or


drive?
There is a scenario, I want to put the class file of A.java source file in classes
folder of c: drive. For example:

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:

e:\sources> javac -d c:\classes Simple.java

To Run:

To run this program from e:\source directory, you need to set classpath of the directory wh
class file resides.

e:\sources> set classpath=c:\classes;.;

e:\sources> java mypack.Simple

Another way to run this program by -classpath switch of java:


The -classpath switch can be used with javac and java tool.

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:

e:\sources> java -classpath c:\classes mypack.Simple

Output:Welcome to package

Ways to load the class files or jar files


There are two ways to load the class files temporary and permanent.
o Temporary
o By setting the classpath in the command prompt
o By -classpath switch
o Permanent
o By setting the classpath in the environment variables
o By creating the jar file, that contains all the class files, and
copying the jar file in the jre/lib/ext folder.

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.

Advantage of static import:


o Less coding is required if you have access any static member of a class
oftenly.

Disadvantage of static import:


o If you overuse the static import feature, it makes the program unreadable
and unmaintainable.

Simple Example of static import

1. import static java.lang.System.*;


2. class StaticImportExample{
3. public static void main(String args[]){
4.
5. out.println("Hello");//Now no need of System.out
6. out.println("Java");
7.
8. }
9. }

What is the difference between import and static import?


The import allows the java programmer to access classes of a package
without package qualification whereas the static import feature allows to
access the static members of a class without the class qualification. The
import provides accessibility to classes and interface whereas static import
provides accessibility to static members of the class.

8
Access Modifiers in Java

The access modifiers in Java specifies the accessibility or scope of a field,


method, constructor, or class. We can change the access level of fields,
constructors, methods, and class by applying the access modifier on it.

There are four types of Java access modifiers:

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.

Understanding Java Access Modifiers


Let's understand the access modifiers in Java by a simple table.

Access within within outside package by outside


Modifier class package subclass only 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.

Example of default access modifier

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.

It provides more accessibility than the default modifer.

Example of protected access modifier

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.

Example of public access modifier

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.

The Java Bean class is the example of a fully encapsulated class.

Advantage of Encapsulation in Java


By providing only a setter or getter method, you can make the class read-
only or write-only.

Simple Example of Encapsulation in Java

1. //A Java class which is a fully encapsulated class.


2. //It has a private data member and getter and setter methods.
3. package com.javatpoint;
4. public class Student{
5. //private data member
6. private String name;
7. //getter method for name
8. public String getName(){
9. return name;
10. }
11. //setter method for name
12. public void setName(String name){
13. this.name=name
14. }
15. }
16. /A Java class to test the encapsulated class.
17. package com.javatpoint;
18. class Test{
19. public static void main(String[] args){
20. //creating instance of the encapsulated class
21. Student s=new Student();

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

1. //A Java class which has only getter methods.


2. public class Student{
3. //private data member
4. private String college="AKG";
5. //getter method for college
6. public String getCollege(){
7. return college;
8. }
9. }

Write-Only class

1. //A Java class which has only setter methods.


2. public class Student{
3. //private data member
4. private String college;
5. //getter method for college
6. public void setCollege(String college){
7. this.college=college;
8. }
9. }

14

You might also like