1

A class from a subpackage needs to access a class from a package one level up. So, I need to keep the target class as public. But I don't want that class to be accessed by any other class. What can I do?

Chinta Jagad
  • 65
  • 1
  • 7
  • `protected` or `default` access modifier would help you with some modification of course. Or using a proxy class. – AxelH Jan 12 '18 at 09:33
  • Packages in java are not hierarchical. So the package "java.util.concurrent" has no relation to "java.util". It is not a sub-package, and might just as well have been "foo.bar.concurrent". – Mark Rotteveel Jan 13 '18 at 13:36

4 Answers4

3

You don't have dozen solutions.
Java is not C++ or OOP languages that have the concept of "friend class".
The protected modifier provides a privileged access for the subclasses but you will not subclass a dependency to access it.
It really makes no sense.

So if you can refactor : move the user class in the same package as the used class and use the private package modifier.

Otherwise, make the class a private nested class of a public factory class that will return a instance of a specific interface matching to the class API you want to protect the access.
In this way, the client classes will only communicate with the interface API.
Of course any class will still to be able to retrieve the instance but will never be coupled with an implementation class.
So in a some way, it protects the access to the class.

FooAPI :

public interface FooAPI {
    void foo();
    //....   
}

FooFactory :

public class FooFactory{

   private MyFactory(){
   } 

   private class MyPrivateFoo implements FooAPI{
        // implements the interface methods 
        @Overrided
        public void foo(){
             // ....
        }
   }  

   public FooAPI of(){
       return new MyPrivateFoo();
   }
}

Edit for your comment :

The used class has public methods which change its state. Only specific classes are allowed to change its state, others are not. How can this be solved ?

The question was not very clear about class access meaning.
With this comment, it is clear.

State changing is a functional behavior.
You want to protect the state change of an instance of a specific class ?
So make this class responsible to decide whether the state should change or not.
Don't provide public methods that allow to do it directly.
You have to change your design.
For example introduce a public doProcessing() method in the class with the state you want to control and make this method responsible to change the state instance if relevant.
You can be interested in Domain Model design that gives a natural and straight way to address this kind of issue.

And to ensure the validity of a behavior, you have a powerful tool : the unit and integration tests.
So use them.

davidxxx
  • 104,693
  • 13
  • 159
  • 179
0

Use protected modifier so the subclass and the package level classes can alone access it

PLease refer to the link for details on access levels

https://www.programcreek.com/2011/11/java-access-level-public-protected-private/

Aarish Ramesh
  • 5,321
  • 10
  • 49
  • 97
0

Refactor your code, depending on your concrete scenario:

  • Make the class in the subpackage inherit from the class a level up.
  • Create an interface in the subpackage that provides the methods you want to access and let the class in the package implement it.
  • Move one of the classes.
Max Vollmer
  • 8,102
  • 9
  • 27
  • 43
0

A class can not be private or protected. The only access modifiers of class is public or no-modifier. But you can use public, private, protected or no-modifier as inner class.

A class can access all properties of another class in same package except private modifier.

Keep all properties as protected in parent class. So, only child class from same or other package can access the parent class properties. Other class will not be able to access.

emon
  • 923
  • 12
  • 14