Inheritance
View MarkdownUse 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.
Declare an object’s supertype
Section titled “Declare an object’s supertype”import com.alexstyl.prosakt.Visibilityimport 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 : IndicationQualified references participate in automatic import resolution, including name-collision handling.
Inherit and override members
Section titled “Inherit and override members”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 : GreetingProviderclass Greeter : NamedGreetingProvider { override fun greeting(): String { return "Hello" }}Multiple and generic supertypes
Section titled “Multiple and generic supertypes”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 Namedinterface Names : List<String>, NamedCall a superclass constructor
Section titled “Call a superclass constructor”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.Baseimport example.Marker
class Child : Base(name = "Alex"), Markerobject 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.
Current limits
Section titled “Current limits”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.