Let’s discover Kotlin — Part 2

Before you start reading this article, you may start by reading the first part: “Let’s discover Kotlin — Part 1”

So let’s go deeper in Kotlin and talk about interfaces, classes, subclasses, enum and many related stuff.

Interfaces

Kotlin interfaces are similar to those of Java 8, they can contain:

  • Abstract methods.
  • Implementation of non-abstract methods (default methods on Java 8).

You have to use interface keyword to declare an interface

interface Shape {
fun area() : Double
}

In this interface we have a single abstract method named area. This method calculates the area of a Shape and return the result (Double).

The implementation of this interface is very simple :

class Circle : Shape {
val radius = 3

override fun area(): Double = 3.14 * (radius * radius)
}

The “override” keyword is used to mark that this method overrides a method from an interface or a superclass. This keyword is mandatory and your code will not compile if you forget to explicitly mark the method as override.

An interface method can have a default implementation, for example :

interface Shape {
fun area() : Double
fun printArea() = println("Area equals : " + area());
}

When you implement the “Shape” interface, you must provide an implementation of the “area” method, but you can use the default implementation of the “printArea” method or override it if needed.

Subclasses

By default, on Kotlin when you declare a new class, this class will be final. This mean that this class can’t be subclassed. This protect your class from having side effect or unexpected changes when subclassed.

To allow the creation of subclasses, you need to explicitly mark a class, properties and methods with “open” keyword.

open class Employee {

//This method can be override by a subclass
fun getEmployeeInformation() {}

//This method can be override by a subclass
open fun getEmployeeSalary() {}
}

When you override a member of a base class or interface, this member will be open by default. If you want to change this behavior, you can add “final” keyword to this member.

Abstract classes

You can declare abstract classes that can contain :

  • Abstract methods that must be overriden in subclass (these methods are open so you don’t need that keyword).
  • Open functions that are not abstract (contain already an implementation), but can be overriden in subclass.
  • Final functions with their implementation (can’t be overriden).
abstract class Shape {
abstract fun area() : Double
open fun printArea() = println("Area equals : " + area())
}

Enums

You can use Enums as in Java. For example :

enum class RGB {
RED, GREEN, BLUE
}

You can also initialize an Enum

enum class RGB (val color: String) {
RED("Red"), GREEN("Green"), BLUE("Blue")
}

Visibility modifiers

In Kotlin, there is four visibility modifiers:

  • public (default one): The member (property, method …) is visible everywhere.
  • internal: The member (property, method …) is visible only in the module where the class belongs.
  • protected: The member (property, method …) is visible only from subclasses.
  • private: The member (property, method …) is only visible in the class.

This is the second part of these tutorials about Kotlin, You can find the last part here.