Having a parent class named Shading
, which is abstract and a child class named GradientShading
, in java it was so easy to store a child object into a variable of the parent type:
Shading act=null;
act=new GradienShading();
In kotlin I don't know how to do it, because this gives compilation error:
lateinit var act: Shading
act = GradienShading()
Type mismatch. Required: Shading Found: GradienShading
Can anyone help me with this easy thing in kotlin?
Thanks
CodePudding user response:
Presumably you've not set up the type hierarchy correctly. Are you sure Shading
in fact is a supertype of GradientShading
?
The following compiles just fine on my machine.
open class Shading
class GradientShading: Shading()
class Example {
lateinit var act: Shading
fun test() {
act = GradientShading()
}
}