to study inheritance at Cotlin I'm asking you this question because there is a confusing part of the child instance property approach in the parent instance
open class Animal(val size:Int)
class Spider(val poison:Int): Animal(50)
fun main() {
val a1:Animal = Spider(5)
println("posion : ${a1.poison}")
}
where a1 is inaccessible to poison. Because a1 is an animal type
Creating a child type in a parent type in an inheritance relationship?Parent instances only for parent type properties
I learned that it's accessible
By the way,
open class Animal(val size:Int)
class Spider(val poison:Int): Animal(50)
fun main() {
val s1 = Spider(5)
val a1:Animal
a1 = s1
println("posion : ${a1.poison}")
}
Why is this accessible?
a1 was created as an animal type.
I don't understand.
kotlin
fun main() {
val s1 = Spider(5)
// // val a1:Animal
val a1 = s1
println("posion : ${a1.poison}")
}
It's like this, so it's not an animal type. It's accessible because it's a spider type.
© 2024 OneMinuteCode. All rights reserved.