Skip to content

Inheritance

View Markdown

Use supertype {} inside ktClass, ktObject, or ktInterface to declare an inherited type. The block uses the same type DSL as other type declarations: reference() names the type, and argument { type {} } supplies generic type arguments.

import com.alexstyl.prosakt.Visibility
import com.alexstyl.prosakt.ktFile
val source = ktFile {
ktObject("NoIndication") {
visibility = Visibility.Internal
supertype { reference("androidx.compose.foundation.Indication") }
}
}

Generated Kotlin:

import androidx.compose.foundation.Indication
internal object NoIndication : Indication

Qualified references participate in automatic import resolution, including name-collision handling.

Classes and objects can implement inherited members using modifiers = listOf("override"). Interfaces can extend other interfaces using the same supertype {} block.

import com.alexstyl.prosakt.ktFile
val source = ktFile {
ktInterface("GreetingProvider") {
ktFunction("greeting") {
modifiers = listOf("abstract")
returns { type { reference("String") } }
}
}
ktInterface("NamedGreetingProvider") {
supertype { reference("GreetingProvider") }
}
ktClass("Greeter") {
supertype { reference("NamedGreetingProvider") }
ktFunction("greeting") {
modifiers = listOf("override")
returns { type { reference("String") } }
body { literal("Hello") }
}
}
}

Generated Kotlin:

interface GreetingProvider {
abstract fun greeting(): String
}
interface NamedGreetingProvider : GreetingProvider
class Greeter : NamedGreetingProvider {
override fun greeting(): String {
return "Hello"
}
}

Call supertype {} once for each inherited type. Types appear in the order they are declared.

import com.alexstyl.prosakt.ktFile
val source = ktFile {
ktInterface("Named")
ktInterface("Names") {
supertype {
reference("kotlin.collections.List")
argument { type { reference("String") } }
}
supertype { reference("Named") }
}
}

Generated Kotlin:

interface Named
interface Names : List<String>, Named

Use superclass("Base") inside ktClass {} or ktObject {} to inherit from a class and call its constructor. Without a block, the call has no arguments. Inside the optional block, use argument() to supply constructor values or generic type arguments, as with ordinary function calls.

import com.alexstyl.prosakt.ktFile
val source = ktFile {
ktClass("Child") {
supertype { reference("example.Marker") }
superclass("example.Base") {
argument("name") { literal("Alex") }
}
}
ktObject("Default") {
superclass("example.Base")
}
}

Generated Kotlin:

import example.Base
import example.Marker
class Child : Base(name = "Alex"), Marker
object Default : Base()

The superclass appears before implemented interfaces. Only one superclass can be configured per declaration. Base must be inheritable and expose a matching constructor. Available since 0.2.0.

Use supertype {} for type references and superclass() for superclass constructor calls. Supertype delegation, such as Service by delegate, is not supported yet. Kotlin’s inheritance rules still apply to the generated code.