Interfaces in Kotlin.
fun main() {
println(InheritingClass("A name").number2)
}
// Very similar in implementation to Java
interface MyInterface { // No primary constructor () as you never make an instance. Open not required, as extendable by default
val number: Int // Can include properties in interfaces. This variable is currently abstract
// val number2: Int = 50 // Initialising properties not allowed in interfaces. Instead:
val number2: Int
get() = 50 // Declare getter to return fixed value. However, no 'field' available here.
fun myFunction(str: String): String
}
interface MySubInterface: MyInterface {
fun mySubfunction(num: Int): String
}
class anotherSomething(override val number: Int) : MySubInterface { // Implement abstract property
override fun myFunction(str: String): String {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun mySubfunction(num: Int): String {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
}
// A class can extend from another class and implement and interface at the same time:
open abstract class AbstractClass(val name: String) {
open fun printName() = println("The name is $name")
abstract fun aNumber(): Double
}
class InheritingClass(name: String): AbstractClass(name), MyInterface { // both inherits and implements interface
override val number: Int = 5 // Implement abstract property
override fun myFunction(str: String): String {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun aNumber(): Double {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
}
