Access Modifiers - what's the purpose?

旧巷老猫 提交于 2019-12-05 09:13:54

There are thousands of reasons, but I'll quote a few from here and then expand on those:


Hiding the internals of the object protects its integrity by preventing users from setting the internal data of the component into an invalid or inconsistent state.

It is very common for a type to enforce certain invariants (e.g., A person's ID number must always be 8 characters long). If a client has full access to every member of a class, then there's no way you can enforce those constraints. Here's a concrete example:

public class Person
{
    public string Id;

    public void SetId(string newId)
    {
        if(newId.Length != 8)
            throw new InvalidArgumentException("newId");

        Id = newId;
    }
}

There's nothing preventing me from just accessing the Id field and setting it to whatever I want! I could just do this:

Person p = new Person();
p.Id = "Invalid Id";

That's why your private state needs to be private.


A supposed benefit of encapsulation is that it can reduce system complexity, and thus increase robustness, by allowing the developer to limit the inter-dependencies between software component.

Say I develop a class which has 40 methods - 35 of which are needed to wire up the internals of the class and to implement its functionality, and 5 of which are actually important to the client. Now I give you this class for you to use - you look at its public interface and you see 40 methods, most of which are completely irrelevant to you, and you ask me "What the hell is this spaghetti of code??"

In order to make sure the intent of a type is clear, you restrain the access of any members that are not relevant to the client.

Also, more public members = greater public surface = more stuff that needs to be tested = harder to maintain.


As a rule of thumb, try to make your members as private as possible, and then work your way up from there. For example, start with private, and then:

  1. Do/would derived classes need to access this member? If so, promote to protected
  2. Do/would other classes need to access this member? If so, promote to public

Actually modifiers are for limiting Access for modeling like to real life objects. Access Modifiers

  1. private
  2. protected
  3. default
  4. public

public access modifier

Fields, methods and constructors declared public (least restrictive) within a public class are visible to any class in the Java program, whether these classes are in the same package or in another package.

private access modifier

The private (most restrictive) fields or methods cannot be used for classes and Interfaces. It also cannot be used for fields and methods within an interface. Fields, methods or constructors declared private are strictly controlled, which means they cannot be accesses by anywhere outside the enclosing class. A standard design strategy is to make all fields private and provide public getter methods for them.

protected access modifier

The protected fields or methods cannot be used for classes and Interfaces. It also cannot be used for fields and methods within an interface. Fields, methods and constructors declared protected in a superclass can be accessed only by subclasses in other packages. Classes in the same package can also access protected fields, methods and constructors as well, even if they are not a subclass of the protected member’s class.

default access modifier

Java provides a default specifier which is used when no access modifier is present. Any class, field, method or constructor that has no declared access modifier is accessible only by classes in the same package. The default modifier is not used for fields and methods within an interface.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!