Contents

Kotlin Basics - Classes

Contents

Methods

In Java, when you declare class methods, you need to declare them inside the class:

1
2
3
4
5
public class Plane {
    public void fly() {
        System.out.println("I'm flying");
    }
}

In Kotlin, you can declare in two different ways:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class Plane() {
    fun fly() {
        println("I'm flying")
    }
}

// or

class Plane

fun Plane.fly() {
    println("I'm flying")
}

// or

class Plane

fun Plane.fly() = println("I'm flying")

It’s interesting to note that other languages (like Golang) also uses the second form.

Data classes

In Java, you will need to declare your DTO like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
public class Person {
    private String firstName;
    private String lastName;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName() {
        this.lastName = lastName;
    }
}

It’s really verbose, and there is lot of code lines (~19 lines).

In Kotlin, you can define the above class in one line:

1
data class Person(val firstName: String, val lastName: String)

Inheritance

Using the apache common-cli library:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public interface Cmd {
    void execute();
    Option getOption();
    boolean isEnabled(CommandLine line);
}

public abstract AbstractCmd implements Cmd {
    @Override
    public boolean isEnabled(CommandLine line) {
        Option option = getOption();
        String longOpt = option.getLongOpt();
        return line.hasOption(longOpt);
    }
}

public HelpCmd extends AbstractCmd {
    private Option option = new Option("h", "help", false, "display the help and exit");

    @Override
    public void execute() {
        System.out.println("Display help here");
    }

    @Override
    public Option getOption() {
        return option;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
interface Cmd {
    fun execute()
    fun getOption(): Option
    fun isEnabled(line: CommandLine): Boolean
}

// Keyword "implements" not needed
abstract class AbstractCmd: Cmd {
    override fun isEnabled(line: CommandLine): Boolean {
        return line.hasOption(getOption().longOpt)
    }
}

// Keyword "extends" not needed
class HelpCmd: AbstractCmd() {
	// We need to declare as "private" and override the `getOption` method otherwise we have an "accidental override" error
    private val option = Option("h", "help", false, "display the help and exit")
    override fun getOption(): Option = option

    // No need to add the character '@' and by default, the methods are public
    override fun execute() {
        println("Display help here")
    }
}