Home Access levels of classes in java
Reply: 4

Access levels of classes in java

user8557976 Published in 2018-01-12 09:29:11Z

This question already has an answer here:

  • In Java, difference between package private, public, protected, and private 25 answers

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?

Aarish Ramesh
Aarish Ramesh Reply to 2018-01-12 09:33:06Z

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


Max Vollmer
Max Vollmer Reply to 2018-01-12 09:35:42Z

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.
davidxxx Reply to 2018-01-12 10:12:17Z

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 
        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.

emon Reply to 2018-01-12 10:28:23Z

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.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.323557 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO