Visibility Workbook


Practice problems for Kotlin Has a Visibility Level Java Doesn’t: internal. Each takes a minute or two. Write your own answer first, then click Show answer — nothing here is a trick question, just direct practice of the syntax from the lesson.

defaults and the basics

1. Declare without a modifier

Declare a top-level class and a top-level function with no visibility modifier, and note (as comments) what visibility they get.

Show answer Hide answer
class Service      // public by default
fun helper() { }   // public by default

In Kotlin the default is public, so you annotate to restrict, not to expose.

2. Hide a member

Give Account a balance property that is visible only inside the class.

Show answer Hide answer
class Account {
    private var balance = 0
}

3. Read-public, write-private

Declare Counter whose value can be read anywhere but written only inside the class.

Show answer Hide answer
class Counter {
    var value: Int = 0
        private set
}

internal and protected

4. Module-only

Declare a class Engine that is usable anywhere in the same module but invisible to code that depends on the module.

Show answer Hide answer
internal class Engine

internal is the level Java has no equivalent for.

5. For subclasses only

Give an abstract class Repository a connect() method visible to subclasses but not to outside callers.

Show answer Hide answer
abstract class Repository {
    protected abstract fun connect(): Connection
}

Unlike Java, Kotlin’s protected does not also leak to the package.

6. File-private

Declare a top-level helper function that’s visible only within its own file.

Show answer Hide answer
private fun audit() { }

For a top-level declaration, private means file scope.

constructors and structure

7. A private constructor

Give class Email a private constructor and a companion of(raw: String): Email? factory that returns null for input without an @.

Show answer Hide answer
class Email private constructor(val address: String) {
    companion object {
        fun of(raw: String): Email? =
            if ("@" in raw) Email(raw) else null
    }
}

8. Many declarations, one file

Write a single file Geometry.kt holding two classes and a top-level function — something Java’s one-class-per-file rule forbids.

Show answer Hide answer
// Geometry.kt
class Point(val x: Int, val y: Int)
class Line(val from: Point, val to: Point)
fun distance(a: Point, b: Point): Double = 0.0

9. Package and import

Write the top two lines of a file: declare it in package geometry, then import kotlin.math.sqrt.

Show answer Hide answer
package geometry
import kotlin.math.sqrt

The package need not mirror the directory path (matching them is just convention).

10. Three levels in one type

Declare class Token(val id: String) with a public id, an internal fun validate(), and a private secret field.

Show answer Hide answer
class Token(val id: String) {
    private val secret = id.hashCode()
    internal fun validate() = secret != 0
}

Each member exposes the smallest surface that still works.


Back to the lesson, Kotlin Has a Visibility Level Java Doesn’t, or on to the next one: delegation.