Visibility modifiers
Visibility modifiers are used to control the access/visibility of variables, functions, and even whole classes. As we will see, it is possible to have variables, functions, and classes with different levels of access depending upon where in the code the access is being attempted from. This allows the designers of a class to practice good encapsulation and make just the functionality and data they choose available to users of the class. As a slightly contrived but useful example, the designers of a class used to talk to a satellite and get GPS data wouldn't allow access to the dropOutOfTheSky
function.
These are the four access modifiers in Kotlin.
Public
Declaring classes, functions, and properties as public
means that they are not hidden/encapsulated at all. In fact, the default visibility is public
and everything we have seen and used so far is, therefore, public. We could make this explicit by using the public
keyword before all our class, function, and property declarations...