Kotlin is a general purpose, free, open source, statically typed “pragmatic” programming language initially designed for the JVM (Java Virtual Machine) and Android that combines object-oriented and functional programming features. It is focused on interoperability, safety, clarity, and tooling support. Following you will find some basic key concepts.
Hello World!
fun main() {
println("Hello, World!")
}
Kotlin is a strongly typed language, but one of the advantages we can found is the ability to do "type inference", it is necessary to always declare types explicitly. The compiler will take care of this.
val & var
var is a mutable variable, can be changed to another value later by reassigning it.
val is a read-only variable (like final variable in Java)
val name = "Kotlin"
println(name)
// var can be initialized later
var anotherName: String
anotherName = "Kotlin var initialized later"
println(anotherName)
// Val MUST be initialized when it is created, it cannot be changed later.
val nameFinal = "Hello Kotlin World!"
println(nameFinal)
Classes & Objects
package com.josias.kotlin.foo
/**
* Foo class to show how to use classes and objects is Kotlin
*/
class Foo {
// property (data member)
private var isOn: Boolean = false
// member function
fun turnOn() {
isOn = true
}
// member function
fun turnOff() {
isOn = false
}
fun displayLightStatus(lamp: String) {
if (isOn == true)
println("$lamp lamp is on.")
else
println("$lamp lamp is off.")
}
}
fun com.josias.kotlin.basics.main(args: Array<String>) {
val l1 = Foo() // create l1 object of Lamp class
val l2 = Foo() // create l2 object of Lamp class
l1.turnOn()
l2.turnOff()
l1.displayLightStatus("l1")
l2.displayLightStatus("l2")
}
The output should be:
> l1 lamp is on.
> l2 lamp is off.
Visibility Modifiers
Not all functions or classes are part of a public API. Some parts of the code are being required to be marked as internal and not accessible from outside of the class or package. They keywords to specify this behavior is called visibility modifier.
There are four modifiers:
- public
- internal
- protected
- private
public is the default if modifier is not provided.
private
Accessed only from the same file. Any private function or property is only visible to other members of the same class, interface, or object:
class Person {
private fun age(): Int = 21
}
internal
Internal deals with the concept of a module. A module is defined as a Maven or Gradle module or an IntelliJ module. Any code that is marked as internal is visible from other classes and functions inside the same module. Effectively, internal acts as public to a module, rather than public to the universe:
internal class Person {
fun age(): Int = 21
}
protected
Top-level functions, classes, interfaces, and objects cannot be declared as protected. Any functions or properties declared as protected inside a class or interface are visible only to members of that class or interface, as well as subclasses.
NULLABILITY - Null syntax
Unfortunately, we have to live with null references as they are present in the JVM, but Kotlin introduces some functionality to make it easier to avoid some common mistakes.
Kotlin requires that a variable that can assigned to null be declared with a ?:
var str: String? = null
Interfaces in Kotlin can contain declarations of abstract methods, as well as method implementations. What makes them different from abstract classes is that interfaces cannot store state. They can have properties but these need to be abstract or to provide accessor implementations.
interface Document {
val version: Long
val size: Long
val name: String
get() = "NoName"
fun save(input: InputStream)
fun load(stream: OutputStream)
fun getDescription(): String {
return "Document $name has $size byte(-s)"}
}
Inheritance
All classes in Kotlin have a common superclass Any, that is the default superclass for a class with no supertypes declared:
class Example // Implicitly inherits from Any
Any has three methods: equals(), hashCode() and toString(). Thus, they are defined for all Kotlin classes.
Visibility modifiers
When you define your class, the contained methods, properties, or fields can have various visibility levels. In Kotlin, there are four possible values:
- Public: This can be accessed from anywhere
- Internal: This can only be accessed from the module code
- Protected: This can only be accessed from the class defining it and any derived classes
- Private: This can only be accessed from the scope of the class defining it
If the parent class specifies that a given field is open for being redefined (overwritten), the derived class will be able to modify the visibility level. Here is an example:
open class Container {
protected open val fieldA: String = "Some value"
}
class DerivedContainer : Container() {
public override val fieldA: String = "Something else"
}
Abstract classes
Adding the abstract keyword in front of the class definition will mark the class as abstract. An abstract class is a partially defined class; properties and methods that have no implementation must be implemented in a derived class, unless the derived class is meant to be an abstract class as well. Here is how you would define an abstract class in Kotlin:
abstract class A {
abstract fun doSomething()
}
Unlike interfaces, you have to mark the function abstract if you don't provide a body definition.
