skip to content

Class Declarations & Members

The mechanics of declaring a class: constructors and init blocks, properties and their backing fields, custom accessors, visibility, and nesting. Interviewers probe here because initialization order and backing-field rules produce real bugs.

part ofKotlinoverview, primer and where to startread it →
on this pageshow

explore

questions

page 2 of 2

How do you control the visibility of a primary constructor and of a property's setter independently in Kotlin?

level: seniorimportance: should knowfreq 45%

basics

~20 s

Put the visibility keyword before the constructor keyword to restrict a primary constructor, e.g. class C private constructor(). For a property, you can give the setter its own visibility like 'var x: Int = 0; private set' so reads are public but writes are private.

open as a page

What exactly is a 'module' for the internal modifier, and when would you choose internal over public or private?

level: seniorimportance: should knowfreq 55%

basics

~20 s

internal means visible everywhere within the same compilation module. A module is a set of files compiled together — like one Gradle source set or IntelliJ module. Use internal to expose things across your module but hide them from outside consumers.

open as a page

How does `init` block timing relate to companion-object initialization and to secondary constructors with `this(...)` delegation?

level: seniorimportance: nice to knowfreq 30%

basics

~20 s

The companion object initializes once when the class is first loaded, before any instance exists. init blocks run for each instance. A secondary constructor first delegates to the primary (running init), then runs its own body.

open as a page

How do secondary constructors behave when a class has no primary constructor, including delegation to a superclass?

level: seniorimportance: nice to knowfreq 30%

basics

~10 s

Without a primary constructor, a secondary constructor can fully initialize the object itself and may call the base class with super(...). There is no this(...) requirement because there is no primary to delegate to.

open as a page

What does the compiler actually generate for nested vs inner classes, and how should that inform your default choice when designing an API?

level: principalimportance: nice to knowfreq 20%

basics

~20 s

A nested class compiles to a standalone (static-like) class with no link to the outer. An inner class compiles to a class with a hidden field pointing at the outer object. Default to nested and only add inner when you truly need that link.

open as a page

How do Kotlin's visibility modifiers map onto JVM bytecode access flags, and why does that matter?

level: principalimportance: nice to knowfreq 30%

basics

~20 s

Kotlin enforces visibility at compile time, then compiles to JVM access flags: public stays public, protected stays protected, but private members are sometimes public in bytecode, and internal becomes public with a mangled name. So Java can sometimes reach what Kotlin hides.

open as a page

showing 31–36 of 36