Create a Java class:
package com.rsk.java; import org.jetbrains.annotations.Nullable; public class Person { private String name; private int age; private Person partner; public String getName() { return name; } public void setName(String name) { if (name == null || name.equals("")) throw new IllegalArgumentException("name"); this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Nullable public Person getPartner() { return partner; } public void setPartner(Person partner) { this.partner = partner; } }
Then create a kotlin class which use Java class:
package com.rsk.kotlin import com.rsk.java.Person import java.lang.Exception class Program { companion object { @JvmStatic fun main(args: Array<String>) { val p = Person() p.name= "WanZ" p.age = 32 println("${p.name}") try { p.name = "" } catch(e: Exception) { println(e.message) } val partner = p.partner println(partner?.name) } } } // Able to extends a Java class class Student: Person() { }