Files and classes

Any Kotlin code will be defined within a file with the .kt extension. A Kotlin file can contain top-level function and property definitions, as well as classes or interfaces. Using the private visibility modifier, it's possible to restrict a piece of code to only being visible within its enclosing file.

The following example illustrates a single file with two classes and a top-level property all defined alongside one another:

// SingleFileExample.kt

val publicProperty = "hello"

class Shape
class Rectangle : Shape()

If a Kotlin file contains only a class definition, it's not required that the class and file share the same name. A single Kotlin file can contain multiple class definitions alongside other top-level properties and functions.