;
Java Basics July 21 ,2025

Access Modifiers & Packages in Java

1. Packages in Java

A package is a namespace that organizes a set of related classes and interfaces. Think of it like a folder in your computer that groups similar files together.

Types of Packages

  1. Built-in Packages – Java provides many packages like java.util, java.io, java.lang, etc.
  2. User-defined Packages – You can create your own packages to organize code better.

A. Creating a Package

To create a package, use the package keyword at the top of the Java file.

package mypackage;

public class Message {
    public void show() {
        System.out.println("Hello from MyPackage!");
    }
}

Save the file as:
mypackage/Message.java

B. Using (Importing) a Package

To use a class from a different package, you need to import it using the import keyword.

import mypackage.Message;

public class Main {
    public static void main(String[] args) {
        Message msg = new Message();
        msg.show();
    }
}

Compile and run from the directory above mypackage:

javac mypackage/Message.java
javac Main.java
java Main

C. Using Static Imports (Optional)

If you want to use static members of a class without class reference, use static import.

import static java.lang.Math.*;

public class Test {
    public static void main(String[] args) {
        System.out.println(sqrt(25));  // instead of Math.sqrt(25)
    }
}

2. Access Modifiers in Java

Access modifiers define the visibility or scope of classes, methods, variables, and constructors. Java provides four access modifiers:

 A. Public

The public access modifier makes a class, method, constructor, or variable accessible from anywhere in the application, including:

  • The same class
  • Other classes in the same package
  • Classes in different packages
  • Subclasses (whether in the same or different package)

1. Accessing public Members in the Same Class

public class Demo {
    public int number = 10;

    public void show() {
        System.out.println("Number: " + number); // Accessible
    }

    public static void main(String[] args) {
        Demo obj = new Demo();
        obj.show(); // Output: Number: 10
    }
}

Explanation:
Since we are accessing public members inside the same class, there are no restrictions.

2. Accessing public Members in the Same Package (Different Class)

// File: PackageOne/Student.java
package PackageOne;

public class Student {
    public String name = "Ravi";

    public void display() {
        System.out.println("Student Name: " + name);
    }
}
// File: PackageOne/Test.java
package PackageOne;

public class Test {
    public static void main(String[] args) {
        Student s = new Student();
        System.out.println(s.name); // Accessible
        s.display();                // Accessible
    }
}

Explanation:
Student and Test are in the same package, so public members of Student are accessible.

3. Accessing public Members in a Different Package (Non-subclass)

// File: PackageOne/Student.java
package PackageOne;

public class Student {
    public String name = "Ravi";

    public void display() {
        System.out.println("Student Name: " + name);
    }
}
// File: PackageTwo/Test.java
package PackageTwo;

import PackageOne.Student;

public class Test {
    public static void main(String[] args) {
        Student s = new Student();
        System.out.println(s.name); // Accessible
        s.display();                // Accessible
    }
}

Explanation:
Even though Test is in a different package, the public class Student and its public members are accessible after importing.

4. Accessing public Members from a Subclass (Different Package)

// File: PackageOne/Person.java
package PackageOne;

public class Person {
    public String nationality = "Indian";

    public void showNationality() {
        System.out.println("Nationality: " + nationality);
    }
}
// File: PackageTwo/Employee.java
package PackageTwo;

import PackageOne.Person;

public class Employee extends Person {
    public void printDetails() {
        System.out.println(nationality); // Accessible
        showNationality();               // Accessible
    }

    public static void main(String[] args) {
        Employee emp = new Employee();
        emp.printDetails(); // Output: Nationality: Indian
    }
}

Explanation:
Even in a different package, a subclass can access public members of its superclass directly.

Summary Table: public Access Modifier

Access LocationAccess to public Members
Same ClassYes
Same Package (Other Class)Yes
Different PackageYes (with import)
Subclass in Any PackageYes

B. private Access Modifier

Definition:

The private modifier makes members accessible only within the same class. They are not accessible from other classes, even in the same package or subclass.

1. Same Class

public class Student {
    private int age = 20;

    private void showAge() {
        System.out.println("Age: " + age);
    }

    public static void main(String[] args) {
        Student s = new Student();
        System.out.println(s.age); // Accessible
        s.showAge();               // Accessible
    }
}

Explanation:
Accessing private members within the same class is allowed.

2. Same Package – Different Class

// File: PackageOne/Student.java
package PackageOne;

public class Student {
    private String name = "Riya";

    private void display() {
        System.out.println("Name: " + name);
    }
}
// File: PackageOne/Test.java
package PackageOne;

public class Test {
    public static void main(String[] args) {
        Student s = new Student();
        // System.out.println(s.name); // Error
        // s.display();                // Error
    }
}

Explanation:
Private members cannot be accessed from other classes, even in the same package.

3. Different Package

// File: PackageOne/Student.java
package PackageOne;

public class Student {
    private int roll = 5;

    private void showRoll() {
        System.out.println("Roll: " + roll);
    }
}
// File: PackageTwo/Test.java
package PackageTwo;

import PackageOne.Student;

public class Test {
    public static void main(String[] args) {
        Student s = new Student();
        // s.showRoll(); // Error: private method
    }
}

Explanation:
Private members are completely hidden from outside the class.

4. Subclass in Different Package

// File: PackageOne/Person.java
package PackageOne;

public class Person {
    private String gender = "Female";

    private void showGender() {
        System.out.println("Gender: " + gender);
    }
}
// File: PackageTwo/Employee.java
package PackageTwo;

import PackageOne.Person;

public class Employee extends Person {
    public void printGender() {
        // System.out.println(gender); // Error
        // showGender();               // Error
    }
}

Explanation:
Private members are not inherited by subclasses and not accessible even indirectly.

C. default Access Modifier (Package-Private)

Definition:

When no access modifier is specified, it is called default access.
Such members are accessible only within the same package.

1. Same Class

public class Student {
    int marks = 85; // default

    void showMarks() {
        System.out.println("Marks: " + marks);
    }

    public static void main(String[] args) {
        Student s = new Student();
        s.showMarks(); // Accessible
    }
}

2. Same Package – Different Class

// File: PackageOne/Student.java
package PackageOne;

class Student {
    int id = 101; // default

    void showId() {
        System.out.println("ID: " + id);
    }
}
// File: PackageOne/Test.java
package PackageOne;

public class Test {
    public static void main(String[] args) {
        Student s = new Student();
        System.out.println(s.id); // Accessible
        s.showId();               // Accessible
    }
}

Explanation:
Default members are accessible within the same package.

3. Different Package

// File: PackageOne/Student.java
package PackageOne;

class Student {
    int age = 18;

    void showAge() {
        System.out.println("Age: " + age);
    }
}
// File: PackageTwo/Test.java
package PackageTwo;

import PackageOne.Student; // Won’t work, class has default access

public class Test {
    public static void main(String[] args) {
        // Student s = new Student(); // Error: not public
    }
}

Explanation:
Classes or members with default access cannot be used outside the package.

4. Subclass in Different Package

Not accessible.

D. protected Access Modifier

Definition:

The protected modifier allows access:

  • Within the same class
  • By other classes in the same package
  • In subclasses, even if they are in different packages

1. Same Class

public class Student {
    protected int rank = 1;

    protected void showRank() {
        System.out.println("Rank: " + rank);
    }

    public static void main(String[] args) {
        Student s = new Student();
        s.showRank(); // Accessible
    }
}

2. Same Package – Different Class

// File: PackageOne/Student.java
package PackageOne;

public class Student {
    protected String course = "Java";

    protected void showCourse() {
        System.out.println("Course: " + course);
    }
}
// File: PackageOne/Test.java
package PackageOne;

public class Test {
    public static void main(String[] args) {
        Student s = new Student();
        System.out.println(s.course); // Accessible
        s.showCourse();               // Accessible
    }
}

3. Different Package – Non-subclass

// File: PackageOne/Student.java
package PackageOne;

public class Student {
    protected int grade = 10;

    protected void showGrade() {
        System.out.println("Grade: " + grade);
    }
}
// File: PackageTwo/Test.java
package PackageTwo;

import PackageOne.Student;

public class Test {
    public static void main(String[] args) {
        Student s = new Student();
        // System.out.println(s.grade); // Error
        // s.showGrade();               // Error
    }
}

Explanation:
Protected members are not accessible in different packages unless through inheritance.

4. Subclass in Different Package

// File: PackageOne/Person.java
package PackageOne;

public class Person {
    protected String city = "Delhi";

    protected void showCity() {
        System.out.println("City: " + city);
    }
}
// File: PackageTwo/Employee.java
package PackageTwo;

import PackageOne.Person;

public class Employee extends Person {
    public void showInfo() {
        System.out.println(city);     // Accessible
        showCity();                   // Accessible
    }

    public static void main(String[] args) {
        Employee e = new Employee();
        e.showInfo(); // Output: City: Delhi
    }
}

✅ Summary Table for All Access Modifiers

Access ModifierSame ClassSame PackageSubclass (Different Package)Other Class (Different Package)
publicYesYesYesYes
privateYesNoNoNo
defaultYesYesNoNo
protectedYesYesYesNo

3. Best Practices

  • Use private to protect data and expose it using getter/setter methods.
  • Use public only when required to make methods or classes globally accessible.
  • Group related classes in packages to modularize your application.
  • Avoid large default access scopes—explicit is better than implicit.

Next Blog- Input/Output in Java

 

Sanjiv
0

You must logged in to post comments.

Get In Touch

123 Street, New York, USA

+012 345 67890

techiefreak87@gmail.com

© Design & Developed by HW Infotech