The following code example demonstrates How to Use Packages and Access Protection in Java.

Using Packages

// Package declaration
package mypackage;

// Class defined in the package
public class MyClass {
    // Public method in the class
    public void publicMethod() {
        System.out.println("This is a public method.");
    }

    // Default (package-private) method in the class
    void defaultMethod() {
        System.out.println("This is a default (package-private) method.");
    }
}
// Another Java file in a different directory
import mypackage.MyClass;

public class PackageDemo {
    public static void main(String[] args) {
        // Create an object of MyClass
        MyClass myObject = new MyClass();

        // Access the public method
        myObject.publicMethod();

        // Access the default method
        myObject.defaultMethod(); // This will result in a compilation error since it's in a different package
    }
}

In this program:

  • We have a package named mypackage that contains the class MyClass. The class has two methods, one marked as public and the other as default (package-private).
  • In a different Java file (PackageDemo.java), we import mypackage.MyClass and demonstrate the usage of the publicMethod method. However, we cannot access the defaultMethod because it is package-private and not visible outside the mypackage.

How to Use Access Protection?

// Access protection demonstration in a single Java file
public class AccessDemo {
    // Private variable
    private int privateVar = 10;

    // Default (package-private) variable
    int defaultVar = 20;

    // Protected variable
    protected int protectedVar = 30;

    // Public variable
    public int publicVar = 40;

    // Public method to access the private variable
    public int getPrivateVar() {
        return privateVar;
    }

    public static void main(String[] args) {
        AccessDemo accessObject = new AccessDemo();

        // Accessing variables and methods from within the same class
        System.out.println("Private Variable: " + accessObject.privateVar);
        System.out.println("Default Variable: " + accessObject.defaultVar);
        System.out.println("Protected Variable: " + accessObject.protectedVar);
        System.out.println("Public Variable: " + accessObject.publicVar);

        // Accessing private variable through a public method
        int privateVarValue = accessObject.getPrivateVar();
        System.out.println("Accessed Private Variable: " + privateVarValue);
    }
}

In this program:

  • We demonstrate different access modifiers (private, default, protected, and public) for variables and methods within a single Java file.
  • We create an instance of the AccessDemo class and access these variables and methods from within the same class.
  • The privateVar variable is accessed indirectly through a public method getPrivateVar.

This program illustrates the usage of access protection modifiers in Java.



Further Reading

Spring Framework Practice Problems and Their Solutions

From Google to the World: The Story of Go Programming Language

Why Go? Understanding the Advantages of this Emerging Language

Creating and Executing Simple Programs in Go

20+ Interview Questions on Go Programming Language

Java Practice Exercise

programmingempire

Princites