In most object-oriented programming languages you’ll encounter the keywords like, public
,private
, protected
or other. These so-called access level modifiers, or just modifiers for short, allow programmers to determine which other classes can access an attribute or method.
Why use Modifiers
Modifiers allow you to structure the code more properly by using some of OOP’s pillars, which are encapsulation and information hiding.
Classes outside of the one you’re programming don’t necessarily need to know what attributes and methods the class has. If your class has an internal method to sort a list which is never used outside of the class, it may be recommended to make them both private.
This allows your class to use them, without letting other classes use them and mess with the state of your class.
The same is valid for getters and setters. They allow fine granular access control on every attribute of your class.
Access Levels
Modifier | Class | Package | Subclass | World |
---|---|---|---|---|
public | Y | Y | Y | Y |
protected | Y | Y | Y | N |
no modifier | Y | Y | N | N |
private | Y | N | N | N |
Most languages at leas feature public, private and some form of protected modifiers. Therefore we’re going to take a look at those.
No modifier usually is the same as private or almost private. But since this changes from language to language, we’re not going to go into that.
Let’s take a look at public, protected and private.
The private and public modifiers
The private modifier is the most restrictive of the modifiers. It allows only the class it belongs to, to be accessed.
This is useful for almost all attributes and some methods. Attributes usually don’t have to be accessible from the outside, and only the methods defined by an interface should be exposed publicly. If no interfaces exist, only the needed methods should be exposed.
If you’re not sure which modifier to use, private is the way to go. Except if you’re sure, the method has to be accessible from the outside.
Usually attributes are private and managed over getters and setters.
Some frameworks may require you to loosen this “rule” up a little bit, like the Unity Engine framework, which needs public attributes instead of getters and setters.
What always needs to be public (except for singletons maybe) is the constructor. Usually that’s the one thing that needs to be public, for other classes to call it.
Also, getters and setters should be public, otherwise no other class can access them.
What about protected
The protected modifier may be confusing at first, but once you dig deeper into design patters you’ll encounter some use cases for them. In particular with the template design pattern, you’ll use protected.
The protected modifer allows subclasses to access the attribute as well. It’s more or less like private but shared accross inheritance.
This allows the subclasses to access and override attributes and methods, which is useful for the template pattern.
In short the template pattern allows you to create a method out of multiple parts. Usually you’ll have an abstract class with a method like this:
public int CalcAge(){
int x = 0;
x = doBefore(x); // protected method
x = x*3; // part that stays the same
x = doAfter(x); // potected method
saveToDB(x);
}
The methods doBefore and doAfter are protected. Which means no other class can access them except the subclasses.
You can now create a new class that extends the abstract one and override
Let me know if you have further questions or correction down in the comments ;).