Packages and imports
A source file should start with a package declaration:
package org.example
fun printMessage() { /*...*/ }
class Message { /*...*/ }
All the contents, such as classes and functions, of the source file are included in this package. So, in the example above, the full name of printMessage() is org.example.printMessage
, and the full name of Message is org.example.Message
.
- A number of packages are imported into every Kotlin file by default.
- Apart from the default imports, each file may contain its own import directives.
Classes
- A class in Kotlin has a primary constructor and possibly one or more secondary constructors.
- If the primary constructor does not have any annotations or visibility modifiers, the constructor keyword can be omitted.
- Kotlin does not have a
new
keyword. - Class members:
- Constructors and initializer blocks
- Functions;
- Properties
- Nested and inner classes
- Object declarations
Abstract Class
- Abstract classes may or may not contain abstract methods.
- Abstract classes cannot be instantiated.
- To use an abstract class, you have to inherit it from another class, provide implementations to the abstract methods in it.
- If you inherit an abstract class, you have to provide implementations to all the abstract methods in it.
Interfaces
Interfaces in Kotlin can contain declarations of abstract methods, as well as method implementations.
Interfaces vs Abstract Class
What makes them different from abstract classes is that interfaces cannot store state. They can have properties, but these need to be abstract or provide accessor implementations.
Functional (SAM) interfaces
An interface with only one abstract method is called a functional interface, or a Single Abstract Method (SAM) interface. The functional interface can have several non-abstract members but only one abstract member.
Object
Object expressions
Such classes are useful for one-time use. Object expressions start with the object keyword.
Functions
- Infix notation:
infix fun Int.shl(x: Int): Int { ... }
, calling the function using the infix notation:1 shl 2
- Local functions: Kotlin supports local functions, which are functions inside other functions.
- Member functions: A member function is a function that is defined inside a class or object.
- Generic functions: Functions can have generic parameters, which are specified using angle brackets before the function name:
fun <T> singletonList(item: T): List<T> { /*...*/ }
- Single-expression function:
fun double(x: Int): Int = x * 2
. - Higher-order functions: a function that takes functions as parameters, or returns a function. A good examplevis the functional programming idiom
fold
for collections. (it is similar toreduce()
in JS) - Function types: Kotlin uses function types, such as
(Int) -> String
. - A lambda expression:
{ a, b -> a + b }
. - An anonymous function:
fun(s: String): Int { return s.toIntOrNull() ?: 0 }
. - Function literals: Lambda expressions and anonymous functions are function literals. They are functions that are not declared but are passed immediately as an expression.
- Function literals with receiver: Function types with receiver, such as
A.(B) -> C,
can be instantiated with a special form of function literals.