avatarGabriel Shanahan

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1643

Abstract

eir setters can have visibility modifiers. Getters always have the same visibility as the property.</p><p id="ece6">There are four visibility modifiers in Kotlin: <code>private</code>, <code>protected</code>, <code>internal</code> and <code>public</code>. The default visibility is <code>public</code>. Kotlin does not have package private visibility, a decision which <a href="https://youtrack.jetbrains.com/issue/KT-29227">is actively debated</a>.</p><p id="0ca4">The only new one is <code>internal</code>, which means that the member is visible within the same module. More specifically, a <i>module</i> is a set of Kotlin files compiled together:</p><ul><li>an IntelliJ IDEA module</li><li>a Maven project</li><li>a Gradle source set (with the exception that the <code>test</code> source set can access the internal declarations of <code>main</code>)</li><li>a set of files compiled with one invocation of the Ant task</li></ul><h1 id="6653">Packages</h1><p id="cfb8">Functions, properties and classes, objects and interfaces can be declared at the “top-level” directly inside a package:</p> <figure id="650c"> <div> <div> <img class="ratio" src="http://placehold.it/16x9"> <iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fpl.kotl.in%2FIjazu43-c&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fpl.kotl.in%2FIjazu43-c&amp;image=https%3A%2F%2Fplay.kotlinlang.org%2Fassets%2Fog-image.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800">

Options

     </div>
        </div>
    </figure></iframe></div></div></figure><h1 id="a4e9">Classes and interfaces</h1><p id="eb75">In Kotlin, and outer class does not see private members of its inner classes (we’ll <a href="https://readmedium.com/nested-and-inner-classes-b300243d036d">talk about those</a> in a bit)</p>
    <figure id="f784">
        <div>
          <div>
            <img class="ratio" src="http://placehold.it/16x9">
            <iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fpl.kotl.in%2FGu0ixxL5Z&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fpl.kotl.in%2FGu0ixxL5Z&amp;image=https%3A%2F%2Fplay.kotlinlang.org%2Fassets%2Fog-image.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="97db">Go back to <a href="https://readmedium.com/interfaces-936fe79a6450">Interfaces</a>, jump to the <a href="https://readmedium.com/table-of-contents-c52573cfa291">Table of Contents</a>, or continue to <a href="https://readmedium.com/nested-and-inner-classes-b300243d036d">Nested and Inner Classes</a>.</p><figure id="8ecd"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*biBSB579iezsNvEQ_NMLBg.png"><figcaption><a href="https://www.etnetera.cz/prace-u-nas?utm_source=medium&amp;utm_medium=GabrielShanahan&amp;utm_campaign=KotlinPrimer&amp;utm_content=join-our-team&amp;utm_term=KotlinPrimer#pozice">Join me in Etnetera</a></figcaption></figure></article></body>

Visibility Modifiers

A brief rundown on visibility modifiers in Kotlin, with an emphasis on internal and what a module is

— — — — — — — — — — — — — — —

THE CURRENT VERSION OF THIS ARTICLE IS PUBLISHED HERE.

— — — — — — — — — — — — — — —

Tags: #FYI

This article is part of the Kotlin Primer, an opinionated guide to the Kotlin language, which is indented to help facilitate Kotlin adoption inside Java-centric organizations. It was originally written as an organizational learning resource for Etnetera a.s. and I would like to express my sincere gratitude for their support.

It is recommended to read the Introduction before moving on. Check out the Table of Contents for all articles.

This is pretty boring, so let’s get it over with. Classes, objects, interfaces, constructors, functions, properties and their setters can have visibility modifiers. Getters always have the same visibility as the property.

There are four visibility modifiers in Kotlin: private, protected, internal and public. The default visibility is public. Kotlin does not have package private visibility, a decision which is actively debated.

The only new one is internal, which means that the member is visible within the same module. More specifically, a module is a set of Kotlin files compiled together:

  • an IntelliJ IDEA module
  • a Maven project
  • a Gradle source set (with the exception that the test source set can access the internal declarations of main)
  • a set of files compiled with one invocation of the Ant task

Packages

Functions, properties and classes, objects and interfaces can be declared at the “top-level” directly inside a package:

Classes and interfaces

In Kotlin, and outer class does not see private members of its inner classes (we’ll talk about those in a bit)

Go back to Interfaces, jump to the Table of Contents, or continue to Nested and Inner Classes.

Join me in Etnetera
Kotlin
Java
Programming
Visibility Modifier
Object Oriented
Recommended from ReadMedium