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

Java Package

Uploaded by

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

Java Package

Uploaded by

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

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.

Here, we will have the detailed learning of creating and using user-defined packages.

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.

4) Packages can be considered as data encapsulation (or data-hiding).

Simple example of java package


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

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. }
Package in Java is a mechanism to encapsulate a group of classes, sub packages
and interfaces. Packages are used for:
 Preventing naming conflicts. For example there can be two classes with
name Employee in two packages, college.staff.cse.Employee and
college.staff.ee.Employee
 Making searching/locating and usage of classes, interfaces,
enumerations and annotations easier
 Providing controlled access: protected and default have package level
access control. A protected member is accessible by classes in the same
package and its subclasses. A default member (without any access
specifier) is accessible by classes in the same package only.
Important points:
1. Every class is part of some package.
2. We can access public classes in another (named) package
using: package-name.class-name

How to access package from another


package?
There are three ways to access the package from outside the package.

1. import package.*;

Example of package that import the packagename.*

//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();
}
}

2. import package.classname;

//save by A.java
package pack;
public class A{
public void msg(){System.out.println("Hello");}
}

//save by B.java
package mypack;
import pack.A;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}

3. fully qualified name.

Example of package by import fully qualified name

//save by A.java
package pack;
public class A{
public void msg(){System.out.println("Hello");}
}

//save by B.java
package mypack;
class B{
public static void main(String args[]){
pack.A obj = new pack.A();//using fully qualified name
obj.msg();
}
}
Note: Sequence of the program must be package then import
then class.

Package inside the package is called the subpackage. It should be created to


categorize the package further. These are not imported by default, they have
to imported explicitly. Also, members of a subpackage have no access
privileges, i.e., they are considered as different package for protected and
default access specifiers.

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: Contains language support classes(e.g classes which defines
primitive data types, math operations). This package is automatically
imported. Contains the collections framework, some
internationalization support classes, properties, random number
generation classes. Classes like ArrayList, LinkedList, HashMap,
Calendar, Date, Time Zone, etc. are part of this package.
2. java.io: Contains classes for supporting input / output operations.
3. java.applet: Contains classes for creating Applets.
4. java.sql: Provides the classes for accessing and processing data stored
in a database. Classes like Connection, DriverManager,
PreparedStatement, ResultSet, Statement, etc. are part of this package.
5. java.lang: Contains classes and interfaces that are fundamental to the
design of the Java programming language. Classes like String,
StringBuffer, System, Math, Integer, etc. are part of this package.
6. java.util:
7. java.net: Provides classes for implementing networking applications.
Classes like Authenticator, HTTP Cookie, Socket, URL,
URLConnection, URLEncoder, URLDecoder, etc. are part of this
package.
8. java.awt: Contains classes for creating user interfaces and for painting
graphics and images. Classes like Button, Color, Event, Font, Graphics,
Image, etc. are part of this package.
User-defined packages: These are the packages that are defined by the user.
First we create a directory myPackage (name should be same as the name of the
package). Then create the MyClass inside the directory with the first statement
being the package names.

Access protection in java packages


-Access Protection is a mechanism that
controls the visibility and accessibility
of class members(fields, methods and
constructors) from other classes.
In java, the access modifiers define the accessibility of the class and its members. For
example, private members are accessible within the same class members only. Java has four
access modifiers, and they are default, private, protected, and public.
In java, the package is a container of classes, sub-classes, interfaces, and sub-packages. The
class acts as a container of data and methods. So, the access modifier decides the accessibility
of class members across the different packages.
In java, the accessibility of the members of a class or interface depends on its access
specifiers. The following table provides information about the visibility of both data members
and methods.
Summary

Access protection in Java controls the visibility and accessibility of class members. Java has
four levels of access protection: public, protected, default, and private. It is important to use
the most restrictive access level that still allows the class to function properly.

Highlights
👀 Public access specifier: Allows access from any class regardless of its package.

Protected access specifier: Allows access from any class in the same package and any
subclass in a different package.

📦 Default access specifier: Allows access from any class in the same package but not from
classes in other packages.

🔒 Private access specifier: Restricts access to only the class that contains it.

📝 Default level of access for class members is package private.

💡 Use the most restrictive access level that still allows the class to function properly.

Key Insights

👀 Public access specifier provides the widest visibility and can be accessed from any class,
making it suitable for methods or fields that need to be accessed globally.

Protected access specifier provides access within the same package and allows subclasses in
different packages to access the member, providing a level of inheritance.

📦 Default access specifier is useful when the member should only be accessible within the
same package, preventing access from other packages.

🔒 Private access specifier ensures that the member can only be accessed within the same class,
providing encapsulation and data hiding.

📝 The default level of access for class members is package private, which allows access
within the same package but not from other packages. It is a good practice to explicitly
specify the access level.

💡 Using the most restrictive access level helps maintain encapsulation, security, and
modularity in the code. It prevents unintentional access and reduces dependencies on specific
implementations.
🔔 The public members can be accessed everywhere.

🔔 The private members can be accessed only inside the same class.

🔔 The protected members are accessible to every child class (same package or other
packages).

🔔 The default members are accessible within the same package but not outside the package.

Example
class ParentClass{
int a = 10;
public int b = 20;
protected int c = 30;
private int d = 40;

void showData() {
System.out.println("Inside ParentClass");
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
}
}

class ChildClass extends ParentClass{

void accessData() {
System.out.println("Inside ChildClass");
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
//System.out.println("d = " + d); // private member can't be accessed
}

}
public class AccessModifiersExample {

public static void main(String[] args) {

ChildClass obj = new ChildClass();


obj.showData();
obj.accessData();

You might also like