Kotlin getters in constructor for inherit | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Kotlin getters in constructor for inherit

I'm trying to understand how Kotlin constructors work. I want classes to inherit the legs and name fields below so I've put them in to the primary constructor. This works. I tried putting get in to the constructor also ... however this causes an exception. Where should the getter go if I want it to be inherited? open class Pet( var legs: Int, var name: String get() = field.toUpperCase() ){ fun SayName(){ println("My name is ${name}") } }

11th Mar 2018, 2:08 PM
Greg Hurlock
Greg Hurlock - avatar
1 Answer
0
you can't put getter on parameters. just put get() function on the class itself
23rd Oct 2018, 4:53 PM
Mohsen Cadir
Mohsen Cadir - avatar