I'm relatively new to programming in general and I was wondering if anyone could help me understand the purpose of access modifiers? I understand the fact that they set different levels of access for classes and variables etc. but why would you want to limit what has access to these? What is the point in not allowing access for different things? why not just allow access for everything?
Sorry if this is a stupid question!
Access modifiers are object-oriented programming that is used to set the accessibility of classes, constructors, methods, and other members of Java. Using the access modifiers we can set the scope or accessibility of these classes, methods, constructors, and other members.
Access Modifiers are keywords used to set visibility for classes, interfaces, methods, data members, and variables. In simple words, the scope of Access Modifiers in Java controls the mentioned features' access levels. They are also referred to as Java Access Specifiers.
default - Visible to the package. No modifiers are needed. private - Visible to the class only. public - Visible to the world.
Access Modifiers or Access Specifiers in a class are used to assign the accessibility to the class members, i.e., they set some restrictions on the class members so that they can't be directly accessed by the outside functions.
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:
protected
public
Actually modifiers are for limiting Access for modeling like to real life objects. Access Modifiers
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With