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.
Here, we will have the detailed learning of creating and using user-defined packages.
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;
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
1. import package.*;
//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();
}
}
//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.
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 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.
💡 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);
}
}
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 {