Clean • Professional
In Java, packages and access modifiers are essential concepts that help you organize and secure your code.
Together, they make your Java program modular, reusable, and secure.
A package is a namespace that organizes related classes and interfaces.
Think of it as a folder in your computer that helps keep your files (classes) organized.
com.myapp.utils
Here:
com → main domain packagemyapp → project nameutils → sub-package (utility classes)Java provides two types of packages:

| Type | Description | Example |
|---|---|---|
| Built-in (Predefined) | Already available in Java API | java.util, java.io, java.lang |
| User-defined | Created by developers | com.myapp.utils, school.student |
Example – Built-in Package
import java.util.Scanner; // importing predefined class
public class InputExample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = sc.nextLine();
System.out.println("Hello, " + name);
}
}
Here, java.util is a built-in package, and Scanner is a class inside that package.
Example – User-defined Package
// File: mypack/Message.java
package mypack;
public class Message {
public void display() {
System.out.println("Welcome to Java Packages!");
}
}
// File: Main.java
import mypack.Message;
public class Main {
public static void main(String[] args) {
Message m = new Message();
m.display();
}
}
Output:
Welcome to Java Packages!
Access modifiers define how accessible a class, method, or variable is from other classes or packages.
There are four main access levels in Java:

private are accessible only within the same class.public class PrivateExample {
private int data = 42;
private void display() {
System.out.println("Private data: " + data);
}
public static void main(String[] args) {
PrivateExample obj = new PrivateExample();
obj.display(); // Accessible here
}
}class TestPrivate {
public static void main(String[] args) {
// PrivateExample obj = new PrivateExample();
// obj.display(); Error - display() is private
}
}package mypack;
class Example {
void show() {
System.out.println("Default access modifier example");
}
}
public class TestDefault {
public static void main(String[] args) {
Example e = new Example();
e.show(); // Works - same package
}
}package anotherpack;
import mypack.Example;
public class TestOutside {
public static void main(String[] args) {
// Example e = new Example(); Error - not accessible
}
}package mypack;
public class Parent {
protected void display() {
System.out.println("Protected method in Parent class");
}
}
package anotherpack;
import mypack.Parent;
class Child extends Parent {
void show() {
display(); // Accessible because Child is a subclass
}
public static void main(String[] args) {
Child c = new Child();
c.show();
}
}
Output:
Protected method in Parent class
public can be accessed from anywhere — inside or outside the package.Example:
package mypack;
public class A {
public void show() {
System.out.println("Public method in class A");
}
}
package anotherpack;
import mypack.A;
public class TestPublic {
public static void main(String[] args) {
A obj = new A();
obj.show(); // Accessible everywhere
}
}
Output:
Public method in class A+-----------------+ +-----------------+
| Package A | | Package B |
|-----------------| |-----------------|
| Class1 (public) | | Class3 (public) |
| Class2 (private)| | Class4 (protected)|
+-----------------+ +-----------------+
Access Levels:
- public: Accessible from anywhere
- protected: Accessible in same package + subclasses
- default: Accessible in same package only
- private: Accessible within class only
// File: company/Employee.java
package company;
public class Employee {
protected int id;
public String name;
public void display() {
System.out.println("ID: " + id + ", Name: " + name);
}
}
// File: office/Main.java
package office;
import company.Employee;
public class Main extends Employee {
public static void main(String[] args) {
Main emp = new Main();
emp.id = 101; // accessible (protected)
emp.name = "John";
emp.display();
}
}
Output:
ID: 101, Name: John
Scenario: Employee Management System
// File: company/Employee.java
package company;
public class Employee {
protected int id; // accessible in subclasses
public String name; // accessible anywhere
public void display() {
System.out.println("ID: " + id + ", Name: " + name);
}
}
// File: office/Main.java
package office;
import company.Employee;
public class Main extends Employee {
public static void main(String[] args) {
Main emp = new Main();
emp.id = 101; // protected → accessible because Main extends Employee
emp.name = "John"; // public → accessible anywhere
emp.display();
}
}Output :
ID: 101, Name: John