Visibility Modifiers

14 Visibility Modifiers

Modifiers are a bit different in Kotlin from how we use them in Java. The default modifier in this language is public , which saves a lot of time and characters. But here it is the long explanation. How do the different visibility modifiers work in Kotlin?

14.1 Modifiers

private

The private modifier is the most restrictive we can use. It implies it will only be visible inside its own file. So if we declare a class as private , we won’t be able to use it outside the file where it was defined.

On the other hand, if we use private inside a class, the access is restricted to that class. Even classes that extend it won’t be able to use it.

So first level classes, objects, interfaces… (known as package members) declared as private are only visible inside the file where they are declared, while everything

defined inside a class or interface will only be visible by that class or interface.

protected

This modifier only applies to members inside a class or an interface. A package member cannot be protected . Inside a member, it works the same way as in Java: it can be used by the member itself and the members that extend it (for instance, a class and its subclasses).

14 Visibility Modifiers

internal

An internal member is visible inside the whole module if it’s a package member. If it’s

a member inside another scope, it depends on the visibility of the scope. For instance, if we write a private class, the access to an internal function will be limited to the visibility of the class.

We can use internal classes from any other class in the same module, but not from another module.

What is a module?

According to Jetbrains definition, a module is a discrete unit of functional- ity which you can compile, run, test and debug independently. It basically refers to the Android Studio modules we can create to divide our project into different blocks. In Eclipse, these modules would refer to the projects inside a workspace.

public

As you may guess, this is the less restrictive modifier. It’s the default modifier, and the member declared as public is visible anywhere, obviously restricted by its scope.

A public member defined in a private class won’t be visible outside the scope where the class is visible.

14.2 Constructors

By default, all constructors are public , which means they can be used from any scope where their class is visible. But we can make a constructor private using this specific syntax:

1 class C private constructor (a: Int) { ... }

14 Visibility Modifiers

14.3 Reviewing our code

We’ve already been making use of the public default modifier, but there are many other visibilities we could change. For instance, in RequestForecastCommand , the property we create from the zipCode constructor parameter could be private.

1 class RequestForecastCommand ( private val zipCode: String)

The thing is that as we are making use of immutable properties, the zipCode value can only be requested, but not modified. So it is not a big deal to leave it as public , and the code looks cleaner. If, when writing a class, you feel that something shouldn’t

be visible by any means, feel free to make it private . Besides, in Kotlin we don’t need to specify the return type of a function if it can be

computed by the compiler. An example of how we can get rid of the returning types:

1 data class ForecastList (...) { 2 operator fun get (position: Int) = dailyForecast[position] 3 }

The typical situations where we can get rid of the return type are when we assign the value to a function or a property using equals (=) instead of writing a code block.

The rest of the modifications are quite straightforward, so you can check them in the repository²⁹ .

²⁹ https://github.com/antoniolg/Kotlin-for-Android-Developers/tree/chapter-14