GarouDan
10/16/2018, 12:40 PMvar parent: MyAbstractClass? = null
how can I override this property in some of the concrete class to have something like:
override var parent: MyConcreteClass? = null
I know that is possible to override properties, but it looks like kotlin do not understand that the MyConcreteClass is a supertype coming from the MyAbstractClass.
Is there a way to get around this?Icaro Temponi
10/16/2018, 12:42 PMAndreas Sinz
10/16/2018, 12:44 PMvar
with a subclass, the following code would fail at runtime:
abstract class MyAbstractClass
class MyConcreteClass : MyAbstractClass()
class MyConcreteClass2 : MyAbstractClass()
open class BaseClass {
var parent: MyAbstractClass? = null
}
class SubClass : BaseClass() {
override var parent: MyConcreteClass? = null
}
val subClass: BaseClass = SubClass()
subClass.parent = MyConcreteClass2() //Is allowed, but fails at runtime
Icaro Temponi
10/16/2018, 12:47 PMabstract class BaseClass {
open val someProp = 1337
abstract var someVar: String
}
class DerivedClass: BaseClass() {
override val someProp = 42
override var someVar = "Hello, World!"
}
Andreas Sinz
10/16/2018, 12:50 PMIcaro Temponi
10/16/2018, 1:00 PMGarouDan
10/16/2018, 1:20 PMVar-property type is 'MyConcreteClass?', which is not a type of overridden
public open var parent: MyAbstractClass? defined in …BaseClass
It’s not needed to me use this line:
subClass.parent = MyConcreteClass2()
But this one will be fine:
subClass.parent = MyConcreteClass()
Should I get the error above? I’m using IntelliJAndreas Sinz
10/16/2018, 1:35 PMAndreas Sinz
10/16/2018, 1:36 PMGarouDan
10/16/2018, 2:05 PMabstract
and I changed MyAbstractClass?
to something like Entity
(a generic that I’m already using). And it works 😃GarouDan
10/16/2018, 2:11 PM@MappedSuperclass
abstract class AbstractTestEntity<Entity : EntityInterface<Entity>> : AbstractEntity<Entity>() {
...
abstract var parent: Entity?
...
}
where Entity is a generic,
so in the concret class I’ve put
@Entity
@Table(name = "tests")
class TestEntity : AbstractTestEntity<TestEntity>() {
....
@JsonIgnore
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "parent_id", insertable = false, updatable = false)
override var parent: TestEntity? = null
...
}
I need the generic for other things, but it helped 😃Andreas Sinz
10/16/2018, 6:25 PMAndreas Sinz
10/16/2018, 6:27 PMTestEntity<MyConcreteEntity>
can only be used as a AbstractTestEntity<MyConcreteEntity>
, not as AbstractTestEntity<MyAbstractEntity>