Tutorials  /  Linux Basics

Kotlin Visibility Modifiers - Tutorial

Ccentron Redaktion · March 2024 ·4 min read ·Linux Basics, Tutorial

In this tutorial, we’ll be discussing the various Visibility Modifiers available in Kotlin programming.

VM

Matching infrastructure at centron

No hardware needed to follow along: ccloud³ VMs with full root access start at €3.12 per month, billed by the hour and ready in seconds. Rent a cloud server →

Kotlin Visibility Modifiers

Visibility Modifiers are modifiers that when appended to a class/interface/property/function in Kotlin, would define where all it is visible and from where all it can be accessed. The setters of properties in Kotlin can have a separate modifier from the property. The getters can’t have a visibility modifier defined. They use the same modifier as that of the property. Following are the visibility modifiers:

  • public
  • protected
  • internal
  • private

Public Kotlin Visibility Modifier

A Public Modifier is the default modifier in Kotlin. Just like the Java public modifier, it means that the declaration is visible everywhere.

Code
class Hello{
}

public class H{
}

fun hi()
public fun hello()

val i = 0
public val j = 5

All the above declarations are the in the top level of the file. ALL are public. If we don’t mention the declaration of the members of the class, they are public(unless they are overridden).

Protected Kotlin Visibility Modifier

A Protected Modifier in Kotlin: CANNOT be set on top-level declarations. Declarations that are protected in a class, can be accessed only in their subclasses.

Code
open class Pr{
    protected val i = 0
}

class Another : Pr{

    fun iValue() : Int
    {
        return i
    }
}

Classes which are not a subclass of Pr cannot access i Declarations that are protected, when overridden would have the same protected modifier in the subclass unless you explicitly change them.

Code
open class Pr{
    open protected val i = 0
}

class Another : Pr(){

    fun iValue() : Int
    {
        return i
    }
    
    override val i = 5 //protected visibility
}

The concept of Protected Modifiers in Kotlin that’s defined above differs from that in Java.

Internal Modifier

Internal is a new modifier available in Kotlin that’s not there in Java. Setting a declaration as internal means that it’ll be available in the same module only. By module in Kotlin, we mean a group of files that are compiled together.

Code
internal class A {
}

internal val x = 0

These won’t be visible outside the current module. Internal Modifiers is useful when you need to hide specific library implementations from the users. This wasn’t possible using the package-private visibility in Java.

Private Modifiers

Private Modifiers do not allow the declarations to be visible outside the current scope.

Code
var setterVisibility: String = "abc"
private set

open class Pr{
    open protected val i = 0

    fun iValue() : Int
    {
        setterVisibility = 10
        return setterVisibility
    }
}

Since kotlin allows multiple top level definitions the above code works. The below doesn’t

Code
private open class ABC{
    private val x = 6
}

private class BDE : ABC()
{
    fun getX()
    {
        return x //x cannot be accessed here.
    }
}

x is visibile only from inside its class. We can set private constructors in the following way:

Code
class PRIV private constructor(a: String) {
 ... 
}

By default classes have public constructors. Wherever the class goes the constructor follows. We need to set the visibility modifier on the constructor in the definition itself. Thus Kotlin uses the protected and internal modifiers differently from Java. Also Java’s default modifier is package-private which doesn’t exist in Kotlin, yet. This brings an end to this quick tutorial on Visibility Modifiers in Kotlin.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Linux Basics
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren