Kotlin Primer
Class and Interface Inheritance
Classes/Interfaces (and their members) in Kotlin are nowhere near as verbose as in Java.
interface Foo
// Defines an interface named `Foo`.
// Nothing in it, but we might just be using it as a Marker to help us classify objects.
open class A
// Defines a class named `A`
// - We can extend it because it's marked `open`
open class B: A(), Foo
// Defines a class named `B`
// - It is a subclass of class `A`
// - It implements interface `Foo`
// - It can be extended because it's `open`
class C: B()
// Defines a class named `C`
// - It is a subclass of `B`
// - Note that this means it's indirectly a subclass of `A` and implements `Foo`
// - It _cannot_ be extended because it is _not_ explicitly marked `open`
fun main() {
val c = C()
// Create an instance of `C` and point value `c` to it.
}
Note
Yes... Kotlin has pointers, just like Java... See Java is Pass-By-Value, Dammit!
Some interesting things to note
- Both interfaces and a class can appear after the colon.
- there can be at most one class, and any number of interfaces, in any order
- Superclasses must be followed by a call to a constructor (the parentheses in
A()andB()on lines 3 and 4)
Before we talk about constructors, let's talk about properties, and I'll circle back. I promise...