OOP in Kotlin
Kotlin is first and foremost an OOP language. We will look at all the basics here:
- Defining packages
- Importing members
- Defining classes and constructors
- Adding members to classes
- Inheritance
- Visibility modifiers
- Singleton and companion objects
- Data classes
- Lambdas and inline functions
Defining packages
Packages are defined with a package statement, which works in a way that is very similar to Java:
package com.example
Unlike Java and Clojure, the source code's directory structure does not have to match the package names used in Kotlin. You are free to organize the source code in any way you wish.
Note
You should not use the package statement in Kotlin's interactive REPL shell as this environment does not support the creation of packages.
Importing members
Kotlin's import statement is very similar to that of Java:
import java.util.ArrayList import java.io.*
One nice difference is that you can specify an alias, which is very handy when you encounter name clashes:
import java.io.File...