Java. Controlling access to classes in packages. Package level access to the class. Public class access level




Controlling access to classes in packages. Package level access to the class. Public class access level


Contents


Search other websites:

1. What are the reasons for the need to properly control access to the contents of libraries in the Java language? Why do You need to use access control correctly?

Proper control of access to the program code means hiding code that can not be used by client programmers, and conversely, the general availability of code that can be used by third-party developers.

In this case, the important is the ability to allocate a variable (changeable) component of the program code from a constant component. Hiding implementation is the basic idea (the main meaning) of access control.

2. What are the levels of access to the classes placed in the packages?

There are two levels of access to a class that is part of a package:

  • ‘public’ access level. In this case, the class is visible from other packages (visible from outside). The word ‘class’ is preceded by the ‘public’ access modifier. The class is accessed using the package name and the name of this class, separated by the symbol ‘ . ‘;
  • the ‘package’ access level. In this case, nothing is written before the word class. Within the package boundaries, the classes from this package are visible to each other.

Manipulating these levels of access, the programmer “hides” or provides in common use the developed program code for its further use by other developers.

3. What does the term “package access” mean?

This is the level of access within the package. It is also called default access. This level of access is established if the access specifier (private, protected, public) is not explicitly specified.

At a packet access level, the class member is available to other classes of the current package. For classes that are implemented in other packages, this class member is considered to be hidden or private.



4. An example that demonstrates a package-level access for two classes

The example declares a project named DemoPackages. The project creates two packages with the names PackageA, PackageB. The PackageA package implements a library of two classes that have the names ClassA1 (located in the ClassA1.java module) and ClassA2 (located in the ClassA2.java module). The initial structure of the project is shown in Figure 1.

Java Eclipse. A fragment of the Package Explorer window. Package structure DemoPackages

Figure 1. A fragment of the Package Explorer window. Package structure DemoPackages

Below are the program codes for the modules (files) ClassA1.java, ClassA2.java, ClassB1.java, ClassB2.java that correspond to ClassA1, ClassA2, ClassB1, ClassB2.

The text of the module ClassA1.java. The class correctly creates an object of class ClassA2. This is logical, because ClassA1 and ClassA2 are declared in one package and have access within the package boundaries.

However, trying to create a ClassB1 class object from another package will cause a compilation error.

package PackageA;

class ClassA1 // there is no explicit public specifier, default access
{
    int a1;

    public static void main(String[] args)
    {
        // access to class Class2 from class Class1 - correctly, classes in one package
        ClassA2 cA2 = new ClassA2();
        cA2.a2 = 109;

        // this code will not compile: classes ClassA1 and ClassB1 in different packages
        // PackageB.ClassB1 cB1 = new PackageB.ClassB1(); // error!
    }
}

The text of the module ClassA2.java. The class correctly creates an object of class ClassA1, because classes are declared in the same package. And of course, it will not work for ClassB1 by abbreviated name, because this class is implemented in another package (PackageB).

package PackageA;

class ClassA2 // there is no explicitly specified public specifier, default access
{
    int a2;

    public static void main(String[] args)
    {
        // access from ClassA2 to ClassA1 - works, both classes in one package
        ClassA1 cA1 = new ClassA1();
        cA1.a1 = 17;

        // PackageB.ClassB1 cB1 = new PackageB.ClassB1(); // and this is a compilation error, classes in different packages
    }
}

The text of the module ClassB1.java. In the module, a ClassB2 class object is correctly created, because the ClassB1 and ClassB2 classes are in the same PackageB package, which is accessed by default. An attempt to create a ClassA1 class object is failed, because the ClassA1 class is declared in another package and there is no public access modifier for it.

package PackageB;

// there is no explicitly specified public specifier, default access (within the scope of the package)
class ClassB1
{
    double b1 = 12.44; // internal variable

    public static void main(String[] args)
    {
        // access to ClassB2 from ClassB1
        ClassB2 cB2 = new ClassB2();
        cB2.b2 = 230;

        // there will be occur a compilation error:
        // PackageA.ClassA1 cA1 = new PackageA.ClassA1(); // error, ClassA1 declared in another package
    }
}

The text of the module ClassB2.java. The class correctly initializes a ClassB1 class variable because ClassB1 and ClassB2 are in the same package.

package PackageB;

// there is no explicitly specified public specifier, default access (within the scope of the package)
class ClassB2
{
    int b2; // internal variable

    // initialization works because the classes are in the same PackageB package
    ClassB1 cB1 = new ClassB1();
}

5. What are the advantages of using access within the package (by default)?

The package access offers the following benefits:

  • interconnected classes are grouped within the boundaries of a single package. These classes easily interact with each other;
  • there is no code redundancy to get to the right classes, which are placed in the same packages. And, therefore, the code becomes more readable;
  • the package code is fully controlled within the package. No one can access classes outside of the package, except for your own classes, which are implemented in the package.





6. What does ‘public’ access type mean for a class implemented in a package?

The class in the package can be publicly available for use in other external libraries. In this case, before the class declaration, the access modifier ‘public’ is specified:

package PackageName;

// ...

// 'public' class declaration
public class ClassName
{
    // ...
}

After such an declaration, you can refer to a class in a package from another package by calling:

PackageName.ClassName

where PackageName – The name of the package in which the public class named PackageName is implemented.

In a package, no more than one ‘public’ class can be declared.

7. An example that demonstrates the use of the public modifier for a class in a package

Let two packages with the names PackageA and PackageB are created in the project named DemoAccess. PackageA implements the ClassA class, which is of package access type (without access modifier). The PackageB package implements the class ClassB, which is declared with the modifier ‘public’.

The structure of the DemoAccess project is shown in Figure 2.

Java Eclipse. The structure of the DemoAccess project

Figure 2. The structure of the DemoAccess project

The ClassA class text from the PackageA package is the following.

package PackageA;

// the class is declared as a package (without the public modifier)
class ClassA
{
    public static void main(String[] args)
    {
        // Access to ClassB class from package PackageB
        // works because the class PackageB.ClassB is declared as public
        PackageB.ClassB cB = new PackageB.ClassB();

        cB.b = 40; // it works, because class variable b is declared as public
    }
}

As you can see from the above code, you can easily access the PackageB.ClassB class, since this class is declared with the public access modifier.

The text of the ClassB.java module, in which the ClassB class is implemented

package PackageB;

public class ClassB
{
    public int b = 20; // the public variable of class

    public static void main(String[] args)
    {
        // access to the class PackageA.ClassA is imposible, because it is declared as a package class
        // PackageA.ClassA cA = new PackageA.ClassA(); - compilation error
    }
}

As you can see from the above code, you will not be able to access the class PackageA.ClassA, since this class is declared as a batch (without access modifier).

It is important. In order to access the internal variable b of ClassB from ClassA, this variable must be declared with a public access modifier.

8. Is it possible to add a private access modifier in the class name declaration?

No it isn’t. The Java language does not support the use of the private access modifier before the class name. That is, the following code is erroneous.

// the word 'private' is forbidden
private class ClassName
{
    // ...
}

This is logical, since to hide a class in a package, a packet-level access level is sufficient, which is declared without any modifier

// package level access to the class
class ClassName
{
    // ...
}

9. Is it possible to add a protected access modifier in the class name declaration?

No it isn’t. The protected access modifier is used for classes that provide their data members and methods for inherited classes. In the inherited classes, protected data members can be used, but they can not be used from objects of these classes (externally). But that’s another topic.


Related topics